I am using some callback function (firebase), the issue that this callback function can be trigger more than once, the issue when I am using setState inside call back then sometimes its not finish to set first state and set the second. that making the setstate without updated data, This is my code:
commentsRef.on("child_changed", (data) => {
if (guests) {
const key = data.key;
const updatedGuest = data.val();
//guests ->isnt updated from last trigger
const cloneGuests =_.cloneDeep(guests);
cloneGuests.forEach((element, index) => {
if (element.guestID === key) {
cloneGuests[index] = updatedGuest;
}
});
setGuests(cloneGuests);
}
});