Last active
September 5, 2015 02:14
-
-
Save thmain/e4a7eccb58e84d14e3f7 to your computer and use it in GitHub Desktop.
Passing context types from parent to children in React.
This file contains 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
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/thmain/e4a7eccb58e84d14e3f7#file-reactcontexttypes-jsx-L24 should be
return { bar: "I am the parent" };