最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far