[英]React useState is not updating value inside Array.map function
I'm having trouble when updating the state inside array.map.更新 array.map 中的 state 时遇到问题。 What i am trying to do is, i get data from the firestore and using the below login;
我想做的是,我从firestore获取数据并使用以下登录名;
const [storedImages, setStoredImages] = useState([]);
(async () => {
setInterval(async () => {
await friends.map(async key => {
const getImagesFromStore= await db
.collection('users')
.doc(key) //XYZ
.collection('uploadedImages')
.get();
const userExistsInList = (users, user) => { //checks if the user exists in storedImages
return users?.hasOwnProperty(user);
};
const newStoryAlreadyInStories = (allImages, newImage) => { //checks if image already exists in the storedImages
return allImages.find(s => s.id === newImage.id);
};
const appendStory = (originalObject, userId, imageToAppend) => {
return {
...originalObject,
[userId]: {
...originalObject[userId],
uploadedImages: originalObject[userId].uploadedImages.concat({
id: imageToAppend.id,
img: imageToAppend.data().image,
timestamp: imageToAppend.data().createdAt,
}),
},
};
};
if (getImagesFromStore && getImagesFromStore.docs.length !== 0) {
if (storedImages.length !== 0) {
const newState = storedImages.map(user => {
if (userExistsInList(user, key)) {
let updatingStory;
getImagesFromStore.docs.forEach(story => {
const found = newStoryAlreadyInStories(stories, story);
if (!found) {
//User is already in state and the new story is not in the list
updatingStory = appendStory(user, key, story);
}
});
return updatingStory;
} else {
//User is not in the list yet
}
});
setStoredImages(newState);
} else {
await getImagesFromStore.docs.map(async image => {
const getUsersDetail = await db
.collection('users')
.doc(key)
.get();
setStoredImages([
{
[key]: {
name: getUsersDetail.data().name,
displayPic: getUsersDetail.data().profileAvtar,
uploadedImages: [
{
id: image.id,
img: image.data().image,
timestamp: image.data().createdAt,
},
],
},
},
]);
});
}
}
});
}, 10000);
})();
The above codes works fine for the first iteration/interval when i use console.log(storedImages) i get the result as expected but after the second iteration/interval i only get the 1st or previous result.当我使用 console.log(storedImages) 时,上述代码对于第一次迭代/间隔工作正常,我得到了预期的结果,但在第二次迭代/间隔后,我只得到第一个或上一个结果。
The state is being updated but the interval function is not aware of this updates. state 正在更新,但间隔 function 不知道此更新。 To get the current state value inside
setInterval
/ setTimeout
function you can use setStoredImages
callback function:要在
setInterval
/ setTimeout
function 中获取当前 state 值,您可以使用setStoredImages
回调 function:
setStoredImages((prev) => {
if (prev.length !== 0) {
//--> your logic
return newState;
} else {
//--> else logic
return [
{
[key]: {
name: getUsersDetail.data().name,
displayPic: getUsersDetail.data().profileAvtar,
uploadedImages: [
{
id: image.id,
img: image.data().image,
timestamp: image.data().createdAt,
},
],
},
},
];
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.