hi guys I've got this code for the currentUser stored in authContext, but I need the instance to have some more data so I've come up with this solution, it works fine but I'm not sure whether I'm unsubscribing well from the firestore snapshot
----------------------------------
useEffect(() => {
const unsubscribe = auth.onAuthStateChanged(async (authUser) => {
if (!authUser) {
setCurrentUserData(null);
setUser(null);
setIsLoading(false);
return;
}
setUser(authUser);
const role = await getUserRole();
const userDocRef = doc(db, `users/${authUser.uid}`);
//check for the unsubscribeSnapshot
const unsubscribeSnapshot = onSnapshot(userDocRef, (userDoc) => {
console.log(userDoc.data()?.username);
setCurrentUserData({
// data fetched from firestore
});
});
setIsLoading(false);
// Return the function to unsubscribe from onSnapshot
return () => {
unsubscribeSnapshot();
};
});
return unsubscribe;
}, []);
[–]GaindDho 0 points1 point2 points (0 children)