[英]TypeError: Cannot destructure property 'position' of 'undefined' as it is undefined
I am passing a function which changes states from Parent Component to Second Component and then Second Component to Third Component and calling from it.我正在传递一个 function ,它将状态从父组件更改为第二组件,然后将第二组件更改为第三组件并从中调用。
This results in the following error:这会导致以下错误:
TypeError: Cannot destructure property 'position' of 'undefined' as it is undefined TypeError:无法解构“未定义”的属性“位置”,因为它未定义
Following is my code for reference:以下是我的参考代码:
export class ParentComponent extends Component {
constructor(props) {
super(props);
this.state = {
address: '',
position: {
lat: 0,
lng: 0,
},
};
this.locationChangeHandler = this.locationChangeHandler.bind(this);
}
locationChangeHandler({ position, address, places }) {
// Set new location
this.setState({ position, address });
}
render(){
<SecondComponent locationChangeHandler={this.locationChangeHandler}/>
}
Second Component:第二部分:
<ThirdComponent locationChangeHandler={locationChangeHandler}/>
Third Component:第三部分:
<FourthComponent onChange={locationChangeHandler()}/>
How do I resolve this?我该如何解决这个问题? I am relatively new to ReactJs and would appreciate all help.
我对 ReactJs 比较陌生,希望得到所有帮助。
can you change locationChangeHandler function like this你能像这样改变locationChangeHandler function吗
locationChangeHandler(loc) {
// Set new location
if(loc && loc.position && loc.address)
this.setState({ position: loc.position, address: loc.address });
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.