I'm trying to map array to get image url from Firebase storage and list them to page. The code give me a url, but the problem is that code not set url to img
variable on time and returning empty img
variable.
return(
<div>
<Header />
{this.state.loaded ? <Loading /> : null}
<div>
{this.state.productsArr.map((product, i) => {
let img = '';
storageRef.child(`images/${product.images[0]}`)
.getDownloadURL()
.then((url) => {
img = url
console.log('then img', img)
})
.catch((err) => console.log(err))
console.log('result',img)
return(
<div key={i}>
<div>
<div>
<img src={img}
alt={product.category}
/>
</div>
</div>
</div>
)
})}
</div>
<Footer />
</div>
)
Where I did mistake? Also get results twice.