Last active
September 2, 2016 07:33
-
-
Save reedho/9fabb796256024dde2a6f0526fdeba9d to your computer and use it in GitHub Desktop.
React Context
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
/* | |
This is the equivalent minimal demo of the above using ES6 class to define React component | |
*/ | |
// Parent | |
class Parent extends React.Component { | |
getChildContext() { | |
return { | |
eventBus: this.props.eventBus, | |
engine: this.props.engine | |
} | |
} | |
static childContextTypes = { | |
eventBus: React.PropTypes.object.isRequired, | |
engine: React.PropTypes.object.isRequired | |
} | |
render() { | |
return <div>{this.props.children}</div> | |
} | |
} | |
// Child | |
class Child extends React.Component { | |
static contextTypes = { | |
engine: React.PropTypes.object.isRequired | |
} | |
render() { | |
return this.props.children; | |
} | |
} | |
// GrandChild | |
class GrandChild extends React.Component { | |
static contextTypes = { | |
eventBus: React.PropTypes.object.isRequired | |
} | |
render() { | |
return <span>this is grand child</span>; | |
} | |
} | |
// root rendering | |
ReactDOM.render( | |
<Parent eventBus={{push: f => f}} engine={{version: "1"}}> | |
<Child> | |
<GrandChild /> | |
</Child> | |
</Parent>, | |
document.getElementById('root') | |
); | |
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
/* | |
As of around late Nov 2014 (possibly starting on react 0.13.0), React now use parent-based context | |
rather than previously owner-based context. | |
React Context (experimental) feature allow parent component to pass data through | |
child components anywhere deeps in the hierarchy without having wired it manually via props. | |
The following minimal demo shows how parent-based context in action. | |
*/ | |
// First, in the parent node it must provide `getChildContext` and `childContextTypes` | |
var Parent = React.createClass({ | |
displayName: 'Parent', | |
getChildContext: function() { | |
return { | |
eventBus: this.props.eventBus, | |
engine: this.props.engine | |
} | |
}, | |
childContextTypes: { | |
eventBus: React.PropTypes.object.isRequired, | |
engine: React.PropTypes.object.isRequired | |
}, | |
render: function() { | |
return <div>{this.props.children}</div> | |
} | |
}); | |
// Second, anywhere in the child or grand grand child, provide `contextTypes` for items it were interested on. | |
var Child = React.createClass({ | |
displayName: 'Child', | |
contextTypes: { | |
engine: React.PropTypes.object.isRequired | |
}, | |
render: function() { | |
return this.props.children; | |
} | |
}); | |
// And this is from grand child | |
var GrandChild = React.createClass({ | |
displayName: 'GrandChild', | |
contextTypes: { | |
eventBus: React.PropTypes.object.isRequired | |
}, | |
render: function() { | |
return <span>this is grand child</span>; | |
} | |
}); | |
// This is the final wiring, the root rendering | |
ReactDOM.render( | |
<Parent eventBus={{push: f => f}} engine={{version: "1"}}> | |
<Child> | |
<GrandChild /> | |
</Child> | |
</Parent>, | |
document.getElementById('root') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using ES6 class that do not support
static
, --probably static was in ES7,the static part must be moved outside class declaration, e.g. for the
Parent
class, thechildContextTypes
must be written like:The same things for
contextTypes
must be done onChild
andGrandChild
class.