This is my useEffect()
:
const [imagesArray, setImagesArray] = useState([]);
useEffect(() => {
console.log("rendering");
let tempImagesArray = [];
storageRef
.listAll()
.then((res) => {
res._delegate.items.forEach(function (imageRef) {
projectStorage
.ref(imageRef._location.path_)
.getDownloadURL()
.then((url) => {
//url is the URL of an image in the database. I have checked, IT is correct
tempImagesArray.push(url);
});
});
})
.catch((err) => {
console.log(err);
});
setImagesArray([...imagesArray, ...tempImagesArray]);
console.log("Temp images arr is", tempImagesArray); //["https://firebase....","https://firebase....", "https://firebase...."]
console.log("Main images state arr is ", imagesArray); //[]
}, []);
I'm adding URLs to tempImagesArray
and after I have added all URLs, I try to update the imagesArray
using setImagesArray
but when I console log imagesArray
, it shows an empty array AND if I console log tempImagesArray
it shows an array which is full of URLs!!!
Why is this happeneing and how do I fix this? Please help, I have been trying to solve this for hours