Last active
March 26, 2019 22:57
-
-
Save alexandrzavalii/8fd0fe01def1c94ecf172466404f3160 to your computer and use it in GitHub Desktop.
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
// create Context | |
const initialAmountOfLollipops = 0; | |
const LollipopContext = React.createContext(initialAmountOfLollipops); | |
/* ... Dumbo's parent Component */ | |
const { lollipops } = this.state; | |
return ( | |
// instead of passing lollipops to Dumbo, we pass it to the Context.Provider | |
<CandyContext.Provider value={lollipops}> | |
<Dumbo availableShoes={availableShoes} /* lollipops={lollipops} */ /> | |
</CandyContext.Provider> | |
); | |
/* ... Dumbo Component */ | |
render () { | |
const { availableShoes } = this.props; | |
const result = crazyComplexMethod(availableShoes); | |
return ( | |
<div> | |
<FirstChild clothing={this.state.firstChild} /> | |
</div> | |
); | |
} | |
// Use Consumer to read the value which we are passing from Provider | |
const FirstChild = props => ( | |
<CandyContext.Consumer> | |
{value => ( | |
<p> | |
I am John, and I am wearing {props.clothing} today. | |
{value ? "thanks for lollipop" : "I want lolipop"} | |
</p> | |
)} | |
</CandyContext.Consumer> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment