简体   繁体   English

TypeError:无法读取 React 中未定义的属性“多对象中的状态名称”

[英]TypeError: Cannot read property 'state name in much object' of undefined in React

So I tried a bunch of object for some state stuff, lets say this was in this.state:所以我尝试了一些 object 的一些 state 的东西,假设这是在 this.state 中:

this.state = {
 musicinmedia: {
  isDetected: false,
  metadata: {
   songname: '',
   artistname: '',
   albumname: '',
  }
 }
}

and this in render:这在渲染中:

{this.state.musicinmedia.metadata.songname}

and gave me an error of: TypeError: Cannot read property 'songname' of undefined并给了我一个错误: TypeError: Cannot read property 'songname' of undefined

Your state might not be initialized, so you need to handle a null/undefined value until it is.你的 state 可能没有被初始化,所以你需要处理一个 null/undefined 值,直到它被初始化。 You could do something like this to make sure state exists.你可以做这样的事情来确保 state 存在。

{this.state && this.state.musicinmedia.metadata.songname}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应:类型错误:无法读取未定义的属性“名称” - REACT: TypeError: Cannot read property 'name' of undefined 使用酶进行反应测试TypeError:无法读取未定义的属性“state” - React test with enzyme TypeError: Cannot read property 'state' of undefined 类型错误:无法读取未定义的属性“状态”。 更新反应 - TypeError: Cannot read property 'state' of undefined. Updated React 未捕获的TypeError:无法读取未定义的反应状态项的属性'toUpperCase' - Uncaught TypeError: Cannot read property 'toUpperCase' of undefined react state item 反应未处理的拒绝(TypeError):无法读取未定义的属性“状态” - React Unhandled Rejection (TypeError): Cannot read property 'state' of undefined 反应 State:未捕获的类型错误:无法读取未定义的属性“值” - React State: Uncaught TypeError: Cannot read property 'value' of undefined react:uncaught TypeError:无法读取未定义的属性“state” - react: uncaught TypeError: Cannot read property 'state' of undefined TypeError:无法读取未定义 React state 的属性“图像” - TypeError: Cannot read property 'image' of undefined React state 未捕获的类型错误:无法读取未定义的属性“状态”-React.js - Uncaught TypeError: Cannot read property 'state' of undefined - React.js 映射对象抛出错误“类型错误:无法读取未定义的属性‘状态’” - mapping object throws error "TypeError: Cannot read property 'state' of undefined"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM