Skip to content

Instantly share code, notes, and snippets.

@antonybudianto
Created September 10, 2018 10:48
Show Gist options
  • Save antonybudianto/41308344ab2972826c1da56d9db639e7 to your computer and use it in GitHub Desktop.
Save antonybudianto/41308344ab2972826c1da56d9db639e7 to your computer and use it in GitHub Desktop.
Modal using portal
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