Skip to content

Instantly share code, notes, and snippets.

@zheeeng
Created April 8, 2018 09:00
Show Gist options
  • Save zheeeng/6f096948bc7271490f30acab6c533286 to your computer and use it in GitHub Desktop.
Save zheeeng/6f096948bc7271490f30acab6c533286 to your computer and use it in GitHub Desktop.
react pattern
// https://github.com/facebook/react/issues/6599
class Modal extends React.Component {
componentDidMount() {
this.node = document.createElement('div');
document.body.appendChild(this.node);
this.renderPortal(this.props);
}
componentWillReceiveProps(nextProps) {
this.renderPortal(nextProps);
}
componentWillUnmount() {
ReactDOM.unmountComponentAtNode(this.node);
document.body.removeChild(this.node);
}
renderPortal = (props) => {
renderSubtreeIntoContainer(this, <ModalPortal {...props} />, this.node);
}
render() {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment