我有一个包含3个组件的应用程序。 第一个是App.jsx 如下它调用TodoList的组分:reactjs从父母编辑孩子状态
<TodoList items={this.state.items} loaded={this.state.loaded} />
的TodoList的组件呈现多个TodoListItem部件
module.exports = React.createClass({
render: function(){
return (
<ul>
{this.renderList()}
</ul>
)
},
renderList: function(){
var children = [];
for(var key in this.props.items) {
if(this.props.items[key].text){
var listItem = this.props.items[key];
listItem.key = key;
children.push(
<TodoListItem item={listItem} key={key} onEdit={this.handleItemEdit} />
)
}
}
return children;
},
handleItemEdit: function(component){
console.log(component);
}
});
在我TodolistItem部件IM
然后渲染多个li元素
module.exports = React.createClass({
getInitialState: function(){
return {
text: this.props.item.text,
done: this.props.item.done
}
},
render: function(){
return (
<li onClick="this.props.onEdit.bind(null,this)">{this.state.text}</li>
)
},
});
当我点击李在父元素函数上的函数handlItemEdit被激发,我的问题是我可以如何改变其父母的handleItemEdit函数中的子元素的文本值? 什么即时试图做的是,当你点击一个李开一个引导模式与输入字段中,更改其文本,保存和通过新的道具TodoListItem
非常感谢您的回答!因为todoList中的道具来自我添加的http请求 componentWillReceiveProps:function(){ this.setState({items:this.props.items}); } 现在在handleItemEdit我怎么只能编辑点击li的值? 我可以通过它的关键吗? –
'handleItemEdit'实际上应该在TodolistItem组件中,而不是在TodoList组件中......这会解决你的问题 –
你也可以阅读这篇[文章](https://facebook.github.io/react/tips/communicate-between- components.html),来介绍如何处理通信的beetwen组件,以另一种方式解决您的问题(如您想要的那样) –