Created
October 23, 2015 22:41
-
-
Save xfsnowind/87979f7a471cdf025f81 to your computer and use it in GitHub Desktop.
communication from parent to child and triggered by child on blog "React -- Communication between components" on 2014/06/24
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
| // For Parent.js | |
| var Parent = React.createClass({ | |
| getInitialState: function () { | |
| return { parentValue: "" }; | |
| }, | |
| // the callback function is passed to Child as props | |
| passValueFunc: function (para) { | |
| if (this.state[para] != undefined) { | |
| return this.state[para]; | |
| } | |
| return ""; | |
| }, | |
| render: function () { | |
| return ( | |
| <div><input type="text" value="Parent"></input> | |
| // pass passValueFunc method as props to Child | |
| <child getparentvalue="{this.passValueFunc}"></child></div> | |
| ); | |
| } | |
| }); | |
| // For Child.js | |
| var Child = React.createClass({ | |
| clickHandler: function () { | |
| // pass parameter to Parent's method can get the handled value back | |
| var parentValue = this.props.getParentValue("parentValue"); | |
| // Handle the parentValue here | |
| }, | |
| render: function () { | |
| return ( | |
| <div><input onclick="{this.clickHandler}/" type="button" value="Child"></input></div> | |
| ); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment