Here's a simplified example (Child to Parent):
var Bar = React.createClass({ validate : function () { var number = React.findDOMNode(this.refs.input).value; this.props.check(number, this.success, this.fail); }, success : function () { alert('Success'); }, fail : function () { alert('Fail'); }, render : function () { return ( <div> <input type='number' min='1' max='20' ref='input'/> <br/> <button onClick={this.validate}>Click me to validate</button> <br/> (Values 1 - 10 are valid, anything else is invalid) </div> ); } }); var Foo = React.createClass({ check : function (number, success, fail) { if (number >= 1 && number <= 10) { success(); } else { fail(); } }, render : function () { return ( <div> <Bar check={this.check} /> </div> ); } });
In this example, <Bar/> is a child, and <Foo/> is a parent. <Bar/> is responsible for processing user input, and when the button is pressed, it calls a function from <Foo/> to perform a check, which, upon completion, calls one of two functions in <Bar/> depending on the results.
Here's what it looks like: http://jsbin.com/feqohaxoxa/edit?js,output
- EDIT -
Here is an example for parent to child:
var Parent = React.createClass({ getInitialState : function () { return({validate : false}); }, click : function () { this.setState({validate : true}); }, done : function () { this.setState({validate : false}); }, render : function () { return ( <div> <button onClick={this.click}>Validate children</button> <br/> <Child num={1} validate={this.state.validate} done={this.done}/> <Child num={2} validate={this.state.validate} done={this.done}/> <Child num={3} validate={this.state.validate} done={this.done}/> </div> ); } }); var Child = React.createClass({ componentWillReceiveProps : function (nextProps) { if (nextProps.validate == true && this.props.validate == false) { var number = React.findDOMNode(this.refs.input).value; if (number >= 1 && number <= 10) { alert("Child " + this.props.num + " valid"); } else { alert("Child " + this.props.num + " invalid"); } this.props.done(); } }, render : function () { return ( <div> <input type="number" min="1" max="20" ref='input'/> </div> ); } });
To do this, I use the state of the parent to indicate whether I want to check the validation. Changing the state from false to true, I force a re-rendering that conveys the truth to the children. When the children receive a new support, they check if it is true and if it is different from the last, and check if it is so. After validation, they use a callback to tell the parent rule to return its validation state to false.
Demo: http://output.jsbin.com/mimaya