Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 反应改变道具巨大的错误_Javascript_Reactjs - Fatal编程技术网

Javascript 反应改变道具巨大的错误

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

我是新手,可能做错了什么

我有一个主要的父组件和他的子组件,以及主组件的子组件2的子组件

主->子对象1->子对象2

我通过道具向child1发送数据。道具项目:

main.js

我只想在搜索函数中重写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);
}