zoukankan      html  css  js  c++  java
  • Portals

    Portals

    Portals 提供了一种很好的将子节点渲染到父组件以外的 DOM 节点的方式。

    const appRoot = document.getElementById('app-root');
    //找一个modal入口挂载点
    const modalRoot = document.getElementById('modal-root');
    
    
    class Modal extends React.Component {
      constructor(props) {
        super(props);
        //创建组件容器
        this.el = document.createElement('div');
      }
    
      componentDidMount() {
        //dom渲染好,将modal容器加入到modalRoot里面
        modalRoot.appendChild(this.el);
      }
    
      componentWillUnmount() {
        //组件销毁到时候删除掉modal容器
        modalRoot.removeChild(this.el);
      }
      
      render() {
        //使用createPortal指定挂载位置
        return ReactDOM.createPortal(
            //接收组件
          this.props.children,
          //将组件加入到modal容器里
          this.el,
        );
      }
    }
    
    
    class App extends React.Component {
      constructor(props) {
        super(props);
        this.state = {showModal: false};
        
        this.handleShow = this.handleShow.bind(this);
        this.handleHide = this.handleHide.bind(this);
      }
    
      handleShow() {
        this.setState({showModal: true});
      }
      
      handleHide() {
        this.setState({showModal: false});
      }
    
      render() {
        const modal = this.state.showModal ? (
          <Modal>
            //传入组件内容
            <div className="modal">
              <div>
                With a portal, we can render content into a different
                part of the DOM, as if it were any other React child.
              </div>
              This is being rendered inside the #modal-container div.
              <button onClick={this.handleHide}>Hide modal</button>
            </div>
          </Modal>
        ) : null;
    
        return (
          <div className="app">
            This div has overflow: hidden.
            <button onClick={this.handleShow}>Show modal</button>
            {modal}
          </div>
        );
      }
    }
    
    ReactDOM.render(<App />, appRoot);
    
    
    
  • 相关阅读:
    汇编学习笔记(25)
    在QT C++中调用 Python并将软件打包发布(裸机可运行)
    集合类
    解决python matplotlib 中文字体的显示乱码-Windows系统;RuntimeWarning: Glyph
    元数据库设计
    解决方案sln工程csproj
    托管代码和非托管代码的区别
    IIS安装和使用URL重写工具-URL Rewrite
    SSL证书及使用
    rpc和rpcwithcert两个虚拟目录
  • 原文地址:https://www.cnblogs.com/pluslius/p/10198155.html
Copyright © 2011-2022 走看看