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

admin2025-04-26  10

I'm building a Modal ponent based on which I used for ReactJS on the web. I attach the Modal to document.body as a child and mount/unmount as needed.

Here is the diagram (Stephen Grider's Udemy online course)

My problem is there is no ReactDOM or document.body in React Native. Here is my 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 {
  oponentDidMount() {
    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
    );
  }

  ponentWillUpdate() {
    this._render();
  }

  ponentWillUnmount() {
    ReactDOM.unmountComponentAtNode(this.modalTarget);
    document.body.removeChild(this.modalTarget);
  }

  render() {
    return <noscript />;
  }
}

I'm using Expo and it's loading App.js by default! And there is no document.body and ReactDOM. How can I solve this problem?

I'm building a Modal ponent based on which I used for ReactJS on the web. I attach the Modal to document.body as a child and mount/unmount as needed.

Here is the diagram (Stephen Grider's Udemy online course)

My problem is there is no ReactDOM or document.body in React Native. Here is my 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 {
  oponentDidMount() {
    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
    );
  }

  ponentWillUpdate() {
    this._render();
  }

  ponentWillUnmount() {
    ReactDOM.unmountComponentAtNode(this.modalTarget);
    document.body.removeChild(this.modalTarget);
  }

  render() {
    return <noscript />;
  }
}

I'm using Expo and it's loading App.js by default! And there is no document.body and ReactDOM. How can I solve this problem?

Share Improve this question asked Oct 14, 2017 at 6:36 merry-go-roundmerry-go-round 4,63513 gold badges59 silver badges112 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

We don't have any DOM in react-native. you should create a modal ponent and import it in other ponents you want to use. your modal's position must be absolute and for showing modal you can pass a prop to it.

you can use react native modal: https://facebook.github.io/react-native/docs/modal.html or any other modals created by people like this : https://github./maxs15/react-native-modalbox

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745673069a313430.html

最新回复(0)