I have a response.js application with a structure like this:
<CalcApp /> --- root component, state is set here <CalcTable /> --- 1st child component <CalcRow /> --- 2nd child component - input onChange event
I want to be able to listen to the onChange event that occurs inside the <-CalcRow-> component, but it is difficult for me to transfer the event down to the root component.
I browsed the web pages and stackoverflow, quite broadly, but did not find examples of how to do this.
What is the best way to pass an event from a deeply nested child component all the way back to the root component so that I can change state?
Here is my complete code:
var React = window.React = require('react'), // include external components from ui folder like the exmaple blow: // Timer = require("./ui/Timer"), Timer = require("./ui/Timer"), mountNode = document.getElementById("app"); var catOne = [ {name : 'one', value : 5, key : 1}, {name : 'two', value : 2, key : 2}, {name : 'three', value : 3, key : 3} ]; var CalcTable = React.createClass({ changeHandler: function(){ console.log('ding'); this.props.onChange(); }, render: function() { var rows = []; // var myVar = this.props.cat1; this.props.cat1.forEach(function(item){ rows.push(<CalcRow item={item} ref="row" key={item.key} onChange={this.changeHandler} />); }); return( <table>{rows}</table> ) } }); var CalcRow = React.createClass({ changeHandler: function(e) { console.log('ping'); this.props.onChange(); }, render: function(){ return( <tr> <td>h</td> <td>{this.props.item.name}</td> <td><input cat1={this.props.item} value={this.props.item.value} name={this.props.item.key} onChange={this.changeHandler} /></td> </tr> ) } }); var AddRowButton = React.createClass({ handleSubmit: function(e) { e.preventDefault(); this.props.onSubmit(this); }, render: function(){ return( <form onSubmit={this.handleSubmit}> <input /> <button>Add</button> </form> ) } }); var SectionSummary = React.createClass({ render: function(){ return( <div className="summary"> <div className="table-summary"> stuff </div> </div> ); } }); var CalcApp = React.createClass({ changeHandler: function(e) { console.log('bong'); }, getInitialState: function(){ return { cat1: this.props.cat1 }; }, handleSubmit: function() { // console.log(this.props.cat1); // console.log(this.props.cat1.length+1); var newKeyVal = this.props.cat1.length+1; c = this.props.cat1; c = c.push({name : "four", value : 4, key : newKeyVal}); this.setState({ cat1:c }); // console.log(this.state.cat1); }, render: function() { return ( <div> <h3>title</h3> <CalcTable cat1={this.props.cat1} onChange={this.changeHandler}/> <div className="stuff"><p>stuff</p></div> <div className="stuff"> <AddRowButton cat1={this.props.cat1} onSubmit={this.handleSubmit}/> </div> <SectionSummary /> </div> ); } }); React.render(<CalcApp cat1={catOne}/>, mountNode);
reactjs
psvj
source share