gpt4 book ai didi

reactjs - 如何通过多个子组件传递事件?

转载 作者:行者123 更新时间:2023-12-03 13:19:06 29 4
gpt4 key购买 nike

我有一个react.js应用程序,其结构如下:

<CalcApp /> --- root component, state is set here
<CalcTable /> --- 1st child component
<CalcRow /> --- 2nd child component
- input onChange event

我希望能够监听 <-CalcRow-> 组件内发生的 onChange 事件,但我在将该事件一直传递到根组件时遇到问题。

我已经相当广泛地浏览了网络和 stackoverflow,但没有找到如何执行此操作的示例。

将事件从深层嵌套的子组件一直传递回根组件以便我可以更改状态的最佳方法是什么?

这是我的完整代码:

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);

最佳答案

使用 pub/sub 时需要注意一点:事件很容易发生过度。起初,它似乎解决了您遇到过的所有问题。过了一会儿,您发现所有代码都是一团纠结的事件意大利面球。

事件有点像全局变量,它们往往会让一切变得更好,直到让一切变得更糟。当你最终陷入让一切变得更糟的境地时,不幸的是你很难回去。

因此请稀疏地使用事件。最好将函数传递给子组件,以便子组件可以通知父组件。这也是一种事件,但更加孤立且更容易跟踪。

你可能会想“那么 Flux 呢,这都是关于事件的?”。它确实如此,但对于如何以及何时发送事件具有相当严格的结构,这使得管理变得更容易。

关于reactjs - 如何通过多个子组件传递事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30420754/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com