Skip to content

Instantly share code, notes, and snippets.

@codeocelot
Last active November 9, 2017 12:11
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 codeocelot/abaad451b7cdfea67adaf9f29240e376 to your computer and use it in GitHub Desktop.
Save codeocelot/abaad451b7cdfea67adaf9f29240e376 to your computer and use it in GitHub Desktop.
Simple React Context Example
class App extends React.Component{
render = () => <div className="app">{this.props.children}</div>
}
class Greeting extends React.Component{
render = () => <p style={this.context.style}>Hello world!</p>
}
Greeting.contextTypes = {
color: React.PropTypes.string,
backgroundColor: React.PropTypes.string
}
class ThemeProvider extends React.Component{
getChildContext = () => ({
style : {
color:'green',
backgroundColor:'black'
}
}),
render = () => {this.props.chidren}
}
ThemeProvider.childContextTypes = {
color: React.PropTypes.string,
backgroundColor: React.PropTypes.string
}
reactDOM.render(
<ThemeProvider>
<App>
<Greeting/>
</App>
</ThemeProvider>,
document.getElementById('root')
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment