Created
September 12, 2018 16:06
-
-
Save icyJoseph/da2037c7a205a522195dee071a58b779 to your computer and use it in GitHub Desktop.
Basic usage of a React Portal
This file contains 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, { 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