Created
December 31, 2016 10:34
-
-
Save bdwain/14c3cdc3dd92370768d647ac28df7cb9 to your computer and use it in GitHub Desktop.
Using Context and ShouldComponentUpdate
This file contains hidden or 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
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