Last active
November 9, 2017 12:11
-
-
Save codeocelot/abaad451b7cdfea67adaf9f29240e376 to your computer and use it in GitHub Desktop.
Simple React Context Example
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
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