Last active
February 1, 2018 00:42
-
-
Save jamesplease/a6396779d28ed91eb1a55d54a5d228c9 to your computer and use it in GitHub Desktop.
Hello
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 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 | |
} | |
}); | |
} | |
} |
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 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