美文网首页现代前端指南!
React v16 中 portal 的使用

React v16 中 portal 的使用

作者: wlianfu | 来源:发表于2017-10-13 10:46 被阅读161次
class Dialog extends React.Component {
  constructor() {
    super(...arguments)

    const doc = window.document
    this.node = doc.createElement('div')
    doc.body.appendChild(this.node)

    window.document.body.appendChild(
      window.document.createElement('div')
    )
  }

  
  componentWillUnmount() {
    window.document.body.removeChild(this.node)
  }

  render() {
    return createPortal(
      <div class='dialog'>
        {this.props.children}
      </div>,
      this.node
    )
  }
}

相关文章

网友评论

    本文标题:React v16 中 portal 的使用

    本文链接:https://www.haomeiwen.com/subject/yexzyxtx.html