Ho un app react.js con struttura come questa:React.js Come passare eventi attraverso componenti figlio multipli?
<CalcApp /> --- root component, state is set here
<CalcTable /> --- 1st child component
<CalcRow /> --- 2nd child component
- input onChange event
voglio essere in grado di ascoltare un un evento onChange accadendo all'interno del -CalcRow-> componente <, ma sto avendo difficoltà passare il evento fino al componente root.
Ho guardato in giro per il web e lo stackoverflow, abbastanza estesamente, ma non ho trovato esempi su come farlo.
Qual è il modo migliore per passare l'evento da un componente figlio profondamente annidato fino al componente radice in modo che possa cambiare stato?
Ecco il mio codice completo:
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);
grazie sto cercando in questo – psvj
Ci sono alcune informazioni più estese su questo sul sito di flusso di Facebook. https: //facebook.github.io/flux/docs/dispatcher.html –
Nel caso in cui qualcuno in una situazione simile lo legga, ho finito per implementare l'architettura del flusso usando reflusso, poiché sembrava essere la più semplice implementazione del flusso. – psvj