1

I can't seem to index to the object. You can see below it shows the object, but when I console out myThumbs or when I try to index something in the object it says undefined.

thumbHelper(){
  let myThumbs = {};
  this.state.apiResponse.fileNames.forEach(f=>{
    this.getThumb(f).then(blob=>{
      myThumbs[f] = blob;
    });
  });
  this.setState({myThumbs});
  console.log(myThumbs);
  console.log(myThumbs["134711.jpg"])
  console.log(this.state.myThumbs);
}

Console Output

React Dev Tools State

enter image description here

Emiel Zuurbier
  • 19,095
  • 3
  • 17
  • 32
  • as u can see it shows the object wehn i console out myThumbs but when i try to index something it says undefined – Hizqeel Javed Sep 06 '20 at 15:40
  • try using callback function `this.setState({myThumbs}, () => console.log(myThumbs["134711.jpg"]));` – Chiller Sep 06 '20 at 15:46
  • I wrote en example here: async thumbHelper() { const fileNames = this.state.apiResponse.fileNames; let myThumbs = {}; const promises = fileNames.forEach((name) => { promises.push(this.getThumb(name)); }); const data = await Promise.all(promises); fileNames.forEach((name, index) => { myThumbs[name] = data[index]; }) this.setState({ myThumbs }); } – RadekDeveloper Sep 06 '20 at 15:53
  • this doesnt work – Hizqeel Javed Sep 06 '20 at 16:05

0 Answers0