简体   繁体   English

TypeError:无法解构“未定义”的属性“位置”,因为它未定义

[英]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.

相关问题 TypeError:无法解构“undefined”的属性“reflectionId”,因为它是未定义的 - TypeError: Cannot destructure property 'reflectionId' of 'undefined' as it is undefined TypeError:无法解构“未定义”或“空”的属性“stat” - TypeError: Cannot destructure property `stat` of 'undefined' or 'null' TypeError:无法解构“orderPay”的属性“加载”,因为它未定义 - TypeError: Cannot destructure property 'loading' of 'orderPay' as it is undefined TypeError:无法解构“未定义”或“空”的属性“queryResult” - TypeError: Cannot destructure property `queryResult` of 'undefined' or 'null' TypeError:无法解构“userUpdate”的属性“加载”,因为它未定义 - TypeError: Cannot destructure property 'loading' of 'userUpdate' as it is undefined TypeError:无法解构“未定义”的属性“list2” - TypeError: Cannot destructure property 'list2' of 'undefined' TypeError:无法解构“对象(...)(...)”的属性“用户”,因为它未定义 - TypeError: Cannot destructure property 'user' of 'Object(...)(...)' as it is undefined × TypeError:无法解构'Object(...)(...)'的属性'currentUser',因为它是未定义的 - × TypeError: Cannot destructure property 'currentUser' of 'Object(...)(...)' as it is undefined TypeError:无法解构“thunkAPI”的属性“rejectWithValue”,因为它未定义 - TypeError: Cannot destructure property 'rejectWithValue' of 'thunkAPI' as it is undefined 类型错误:无法解构“项目”的属性“名称”,因为它未定义 - TypeError: Cannot destructure property 'name' of 'item' as it is undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM