Be the first user to complete this post
|
Add to List |
Using context and childContext in React
Child contexts
that allows a parent element to specify a context - aka a set of properties - which can be accessed from all of its children and grandchildren via the this.context
object.
There are 2 aspects to how you would go about implementing this in your code.
- In the parent, the getChildContext method is used to return an object that is passed own as the child context. The prop types of all the keys in this object must also be defined in the
childContextTypes
property of the parent. - Any child that needs to consume the properties passed down from its parents must whitelist the properties it wants to access via the `contextTypes' property.
contextTypes
on every child component. However, the reality is that you can specify the `contextTypes' property on a mixin. That way you can avoid rewriting the whitelisting boilerplate code by simply using the mixin on all the child components that need to access those properties from its context.
Also Read:
- webpack with babel6 and react
- Passing the store down implicitly via context in a react redux app
- combineReducers in reduxjs explained
- Pass props to the handler component in react-router