在 React Native 中,ReactDOM 和 document.body 的等效变量是什么? (世博会)

What are the equivalent variables for ReactDOM and document.body in React Native? (Expo)

我正在构建一个 Modal 组件,我基于该组件在网络上用于 ReactJS。我小时候将 Modal 附加到 document.body 并根据需要附加 mount/unmount。

这是图表(Stephen Grider 的 Udemy 在线课程)

我的问题是 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。您应该创建一个模态组件并将其导入您要使用的其他组件。您的模态框的位置必须是绝对的,为了显示模态框,您可以向它传递一个道具。

你可以使用 React Native 模式: https://facebook.github.io/react-native/docs/modal.html 或像这样的人创建的任何其他模态: https://github.com/maxs15/react-native-modalbox