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.
Here's an example of how this works
[wpgist id="e4a7eccb58e84d14e3f7" file="ReactContextTypes.jsx"]
One may argue that this makes the code a bit more verbose, because now you end up creating an additional key childContextTypes
property of the parent.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:
- Pure vs Impure functions
- Pass props to the handler component in react-router
- Using React with Backbone Models
- Uncaught TypeError: Cannot read property 'toUpperCase' of undefined in React
- Automating Store and Action registration for multiple components using Fluxxor