Portals in React 16: Is it right to append a popup window in document's body?


(Aleksandr K) #1

Or is there another way?

Example:

class Modal extends React.Component {
  constructor(props) {
    super(props);
    this.el = document.createElement('div');
  }

  componentDidMount() {
    document.body.appendChild(this.el); // <------
  }

  render() {
    return ReactDOM.createPortal(
      this.props.children,
      this.el,
    );
  }
}

(Jed Fox) #2

It should be fine as long as you’re not rendering your root React component directly into <body>, which is a bad idea for several reasons.