Skip to content

Instantly share code, notes, and snippets.

@seamusleahy
Last active May 16, 2018 21:15
Show Gist options
  • Save seamusleahy/c2b26bc32bb3c60d4ab0a83bcc4985ed to your computer and use it in GitHub Desktop.
Save seamusleahy/c2b26bc32bb3c60d4ab0a83bcc4985ed to your computer and use it in GitHub Desktop.
Basics of creating a React Provider. https://seamusleahy.com/how-to-creating-a-react-provider/
<LocalizationProvider messages={myLocalizationMessages}>
<Foo>
<h1><LocalizationText key="welcomeMessage" /></h1>
</Foo>
</LocalizationProvider>
function Providee(props, context) {
const mySharedValue = context.mySharedValue;
const whatIsFoo = mySharedValue.foo;
return (<span>{whatIsFoo}</span>);
}
Providee.contextTypes = {
mySharedValue: React.PropTypes.object
};
class Provider extends React.Component {
// Render the child component
render() {
return this.props.children;
}
// Set the share value
getChildContext() {
return {
mySharedValue: {foo: 'bar'}
};
}
}
// Define the shared value
Provider.childContextTypes = {
mySharedValue: React.PropTypes.object
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment