简体   繁体   中英

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:

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

Your state might not be initialized, so you need to handle a null/undefined value until it is. You could do something like this to make sure state exists.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM