React v16 中 portal 的使用
2017-10-13 本文已影响161人
wlianfu
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
)
}
}