Skip to content

Instantly share code, notes, and snippets.

@icyJoseph
Created September 12, 2018 16:06
Show Gist options
  • Save icyJoseph/da2037c7a205a522195dee071a58b779 to your computer and use it in GitHub Desktop.
Save icyJoseph/da2037c7a205a522195dee071a58b779 to your computer and use it in GitHub Desktop.
Basic usage of a React Portal
import React, { Component, Fragment } from "react";
import Portal from "./Portal";
export default class BasicModal extends Component {
state = { open: false };
componentDidMount() {
this.listener = document.body.addEventListener("click", () => {
this.closeModal();
});
}
openModal = () =>
this.setState({
open: true
});
closeModal = () =>
this.setState({
open: false
});
componentWillUnmount() {
document.body.removeEventListener("click", this.listener);
}
render() {
return (
<Fragment>
<button onClick={this.openModal}> Open Basic Modal </button>
{this.state.open && (
<Portal
children={this.props.children}
closeCallback={this.closeModal}
/>
)}
</Fragment>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment