Created
September 10, 2018 10:48
-
-
Save antonybudianto/41308344ab2972826c1da56d9db639e7 to your computer and use it in GitHub Desktop.
Modal using portal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import css from './Modal.css'; | |
let modalRoot; | |
class Modal extends React.Component { | |
constructor(props) { | |
super(props); | |
this.el = document.createElement('div'); | |
} | |
componentDidMount() { | |
modalRoot = document.getElementById('modal-root'); | |
modalRoot.appendChild(this.el); | |
} | |
componentWillUnmount() { | |
modalRoot.removeChild(this.el); | |
} | |
render() { | |
return ReactDOM.createPortal( | |
<div className={css.Modal}>{this.props.children}</div>, | |
this.el | |
); | |
} | |
} | |
export default Modal; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment