Skip to content

Portal ​

本节目标:理解“Portal”的核心思路,并能把它用于实际开发或面试表达。

  • React v16增加了对Portal的直接支持
  • 它可以把JSX渲染到一个单独的DOM节点中
src\index.js
import React from './react';
import ReactDOM from './react-dom';
class Dialog extends React.Component {
  constructor(props) {
    super(props);
    this.node = document.createElement('div');
    document.body.appendChild(this.node);
  }
  render() {
    ReactDOM.createPortal(
      <div className="dialog">
        {this.props.children}
      </div>,
      this.node
    );
    return null;
  }
  componentWillUnmount() {
    window.document.body.removeChild(this.node);
  }
}
class App extends React.Component {
  render() {
    return (
      <div>
        <Dialog>

模态窗

</Dialog>
      </div>
    )
  }
}
ReactDOM.render(
  <App />, document.getElementById('root'));