Javascript 反应改变道具巨大的错误
我是新手,可能做错了什么 我有一个主要的父组件和他的子组件,以及主组件的子组件2的子组件 主->子对象1->子对象2 我通过道具向child1发送数据。道具项目: main.js 我只想在搜索函数中重写this.props.items,但有一个错误:Javascript 反应改变道具巨大的错误,javascript,reactjs,Javascript,Reactjs,我是新手,可能做错了什么 我有一个主要的父组件和他的子组件,以及主组件的子组件2的子组件 主->子对象1->子对象2 我通过道具向child1发送数据。道具项目: main.js 我只想在搜索函数中重写this.props.items,但有一个错误: × TypeError: Cannot assign to read only property 'items' of object '#<Object>' 我怎样才能正确地更改它?谢谢您不能这样做: this.props.items
×
TypeError: Cannot assign to read only property 'items' of object '#<Object>'
我怎样才能正确地更改它?谢谢您不能这样做:
this.props.items=res;
道具和属性是不可变的对象。只需在组件的状态中创建一个新项:
this.setState({newItems: res});
...
<Find dataChild2={this.state.items} findData={this.search.bind(this)}/>
你不能更新道具,它们是不可变的。如果您希望在顶级父级中操作结果值,而不是在child1中使用搜索方法,请将其移动到父级并在搜索方法中捕获结果- 母公司-
<child1 items={this.state.lst} getData={this.search.bind(this)}>
this.props.items=res;
this.setState({newItems: res});
...
<Find dataChild2={this.state.items} findData={this.search.bind(this)}/>
<child1 items={this.state.lst} getData={this.search.bind(this)}>
<Find dataChild2={this.props.items} findData={this.props.getData.bind(this)}/>
someFunc(){
this.props.findData(result);
}