Created
April 19, 2018 19:59
-
-
Save jpgorman/5413cec925a504887824c9aeec0b9525 to your computer and use it in GitHub Desktop.
Example using context API
This file contains hidden or 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 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