Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active September 5, 2015 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/e4a7eccb58e84d14e3f7 to your computer and use it in GitHub Desktop.
Save thmain/e4a7eccb58e84d14e3f7 to your computer and use it in GitHub Desktop.
Passing context types from parent to children in React.
var Grandparent = React.createClass({
childContextTypes: {
foo: React.PropTypes.string.isRequired
},
getChildContext: function() {
return { foo: "I m the grandparent" };
},
render: function() {
return <Parent />;
}
});
var Parent = React.createClass({
childContextTypes: {
bar: React.PropTypes.string.isRequired
},
getChildContext: function() {
return { bar: "I am the parent" };
},
render: function() {
return <Child />;
}
});
var Child = React.createClass({
contextTypes: {
foo: React.PropTypes.string.isRequired,
bar: React.PropTypes.string.isRequired
},
render: function() {
return (
<div>
<div>My name is: {this.context.foo}</div>
<div>My name is: {this.context.bar}</div>
</div>
)
}
});
// Finally you render the grandparent
React.render(<Grandparent />, document.body);
@Sinewyk
Copy link

Sinewyk commented Jun 28, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment