Skip to content

Instantly share code, notes, and snippets.

@kevinkassimo
Created April 23, 2018 20:49
Show Gist options
  • Save kevinkassimo/c71fb9e7f06d520b50d660b4a361a8ff to your computer and use it in GitHub Desktop.
Save kevinkassimo/c71fb9e7f06d520b50d660b4a361a8ff to your computer and use it in GitHub Desktop.
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