Skip to content

Instantly share code, notes, and snippets.

@jamesplease
Last active February 1, 2018 00:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesplease/a6396779d28ed91eb1a55d54a5d228c9 to your computer and use it in GitHub Desktop.
Save jamesplease/a6396779d28ed91eb1a55d54a5d228c9 to your computer and use it in GitHub Desktop.
Hello
import ModalContext from './modal-context';
class Parent extends Component {
render() {
return (
<ModalContext.Provider value={this.state.modalContext}>
<Child/>
</ModalContext.Provider>
);
}
toggleModal(displayModal) {
this.setState({
modalContext: {
displaying: displayMode,
toggleModal: this.toggleModal
}
});
}
}
import ModalContext from './modal-context';
// Although this was the immediate child in this example, it is better to imagine
// it as the great-great-great-great grandchild, which is why we didn't just use props :)
class Child extends Component {
render() {
return (
<ModalContext.Consumer>
{(displaying, toggleModal) => {
<Fragment>
{displaying && <Modal hide={() => toggleModal(false)}/>}
<button onClick={() => toggleModal(true)}>
Show modal
</button>
</Fragment>
}}
</ModalContext.Consumer>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment