我正在构建一个 Modal
基于我在网络上用于 ReactJS 的组件。我附上Modal
至document.body
作为 child 并根据需要安装/卸载。
这是图表(Stephen Grider 的 Udemy 在线类(class))
我的问题是 React Native 中没有 ReactDOM 或 document.body。 这是我的 modal.py
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { store } from '../store';
import { Provider } from 'react-redux';
class Modal extends Component {
comoponentDidMount() {
this.modalTarget = document.createElement('div');
this.modalTarget.className = 'modal';
document.body.appendChild(this.modalTarget);
this._render();
}
_render() {
ReactDOM.render(
<Provider store={store}>
<div>{this.props.children}</div>
</Provider>,
this.modalTarget
);
}
componentWillUpdate() {
this._render();
}
componentWillUnmount() {
ReactDOM.unmountComponentAtNode(this.modalTarget);
document.body.removeChild(this.modalTarget);
}
render() {
return <noscript />;
}
}
我正在使用 Expo,它默认加载 App.js!而且没有 document.body 和 ReactDOM .我怎么解决这个问题?
最佳答案
我们在 react-native 中没有任何 DOM。您应该创建一个模态组件并将其导入您要使用的其他组件中。您的模态的位置必须是绝对的,并且为了显示模态,您可以将 Prop 传递给它。
你可以使用 react 原生模式:
https://facebook.github.io/react-native/docs/modal.html
或由这样的人创建的任何其他模态:
https://github.com/maxs15/react-native-modalbox
关于javascript - React Native 中 ReactDOM 和 document.body 的等效变量是什么? (世博会),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46741657/