I had this error before
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
This is my useEffect
where I retrieve data from firestore and it does remove the error:
useEffect(async () => {
let isMounted = true;
const querySnapshot = await getDocs(collection(db, "product"));
const arr = [];
querySnapshot.forEach((doc) => {
arr.push({
...doc.data(),
id: doc.id,
});
if (isMounted) setProduct(arr);
});
return () => {
isMounted = false;
};
}, []);
Is the useEffect
alright or will this cause any problems in the future? And am I mounting this right?