Last active
May 16, 2018 21:15
-
-
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/
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
<LocalizationProvider messages={myLocalizationMessages}> | |
<Foo> | |
<h1><LocalizationText key="welcomeMessage" /></h1> | |
</Foo> | |
</LocalizationProvider> |
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
function Providee(props, context) { | |
const mySharedValue = context.mySharedValue; | |
const whatIsFoo = mySharedValue.foo; | |
return (<span>{whatIsFoo}</span>); | |
} | |
Providee.contextTypes = { | |
mySharedValue: React.PropTypes.object | |
}; |
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 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