如果您有一个数组作为状态的一部分,并且该数组包含对象,那么通过更改这些对象之一来更新状态的简单方法是什么?
示例,从关于 react 的教程修改而来:
var CommentBox = React.createClass({
getInitialState: function() {
return {data: [
{ id: 1, author: "john", text: "foo" },
{ id: 2, author: "bob", text: "bar" }
]};
},
handleCommentEdit: function(id, text) {
var existingComment = this.state.data.filter({ function(c) { c.id == id; }).first();
var updatedComments = ??; // not sure how to do this
this.setState({data: updatedComments});
}
}