Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can I access specific index of array in react js [0]

this code works

the code works fine but when I try to access the first index it says "cannot read properties of undefined (reading ‘0’)"

this code doesn't work

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

this is the response of console.log("Dataa", this.state.data.videos)

response

this is the response of console.log("Dataa", this.state.data.videos[0])

enter image description here

>Solution :

This is because you are trying to get an index of undefined. Before setting the state to data, the state key ‘videos’ is undefined. You can try in render:

if (this.state.data.videos) {
  console.log('Data', this.state.data.videos[0])
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading