Skip to content

Instantly share code, notes, and snippets.

@bdwain
Created December 31, 2016 10:34
Show Gist options
  • Save bdwain/14c3cdc3dd92370768d647ac28df7cb9 to your computer and use it in GitHub Desktop.
Save bdwain/14c3cdc3dd92370768d647ac28df7cb9 to your computer and use it in GitHub Desktop.
Using Context and ShouldComponentUpdate
class ThemeProvider extends React.Component {
constructor(p, c) {
super(p, c)
this.getColor = this.getColor.bind(this)
}
getColor(){
return this.props.color //could be this.state.color
}
getChildContext() {
return {getColor: this.getColor}
}
render() {
return <div>{this.props.children}</div>
}
}
ThemeProvider.childContextTypes = {
getColor: React.PropTypes.func
}
class ThemedText extends React.Component {
render() {
return <div style={{color: this.context.getColor()}}>
{this.props.children}
</div>
}
}
ThemedText.contextTypes = {
getColor: React.PropTypes.func
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment