A Pen by Etienne Deladonchamps on CodePen.
Created
September 10, 2017 09:39
-
-
Save etienne-dldc/13d020a206c79a14e7738c7ed90495f6 to your computer and use it in GitHub Desktop.
15 - Context
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
<div id="app"></div> |
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 LocaleProvider extends React.Component { | |
getChildContext() { | |
return { locale: "fr" }; | |
} | |
render() { | |
return this.props.children; | |
} | |
} | |
LocaleProvider.childContextTypes = { | |
locale: PropTypes.string | |
}; | |
class LocalizedHello extends React.Component { | |
render () { | |
if (this.context.locale === 'fr') { | |
return <span>Bonjour !</span>; | |
} | |
return <span>Hello !</span>; | |
} | |
} | |
LocalizedHello.contextTypes = { | |
locale: PropTypes.string | |
} | |
const App = () => ( | |
<LocaleProvider> | |
<LocalizedHello /> | |
</LocaleProvider> | |
); | |
ReactDOM.render( | |
<App/>, | |
document.getElementById('app') | |
); |
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
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/prop-types/15.5.10/prop-types.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment