Created
April 23, 2018 20:49
-
-
Save kevinkassimo/c71fb9e7f06d520b50d660b4a361a8ff 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
const ColorContext = React.createContext(); | |
class App extends React.Component { | |
constructor(props) { | |
super(props); | |
this.updateColor = (newColor) => this.setState({ color: newColor }) | |
this.state = { | |
color: 'red', | |
updateColor: this.updateColor, | |
}; | |
} | |
render() { | |
return ( | |
<ColorContext.Provider value={this.state}> | |
<Button /> | |
</ColorContext> | |
); | |
} | |
} | |
class Button extends React.Component { | |
render() { | |
return ( | |
<ColorContext.Consumer> | |
{(context) => | |
<button | |
style={{ color: context.color }} | |
onClick={() => context.updateColor('green')}> | |
Click Me! | |
</button> | |
} | |
</Consumer> | |
); | |
} | |
} | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment