Skip to content

Instantly share code, notes, and snippets.

@jpgorman
Created April 19, 2018 19:59
Show Gist options
  • Save jpgorman/5413cec925a504887824c9aeec0b9525 to your computer and use it in GitHub Desktop.
Save jpgorman/5413cec925a504887824c9aeec0b9525 to your computer and use it in GitHub Desktop.
Example using context API
class DataProvider extends React.Component {
getChildContext() {
// we're omitting children from our props and passing all other props into context
const {children, ...rest} = this.props
return {
// we'll default to an empty object
state: rest || {}
};
}
render() {
return this.props.children;
}
}
// remembering to add our context type
DataProvider.childContextTypes = {
state: PropTypes.any
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment