react-query-firebase
react-query-firebase copied to clipboard
Using invalidateQueries function makes useFirestoreQueryData to return old data
I'm trying to optimistically update a document in firestore, but on calling the invalidateQueries function once useMutation is settled I'm getting the old document data, though the document has been updated in the firebase. Also initially I get the updated data but after that, it gets replaced with the old data.
onMutate: async ({ currData }) => {
// Cancel any outgoing refetches (so they don't overwrite our optimistic update)
await queryClient.cancelQueries(["auth-user"]);
// Snapshot the previous value
const previousUserData = queryClient.getQueryData(["auth-user"]);
// Optimistically update to the new value
queryClient.setQueryData(["auth-user"], () => currData);
// Return a context object with the snapshotted value
return { previousUserData };
},
// If the mutation fails, use the context returned from onMutate to roll back
onError: (_error, _passedUser, context) => {
console.log("error", _error);
toast.show({
title: "Couldn't update user profile!",
});
queryClient.setQueryData(["auth-user"], context?.previousUserData);
},
// Always refetch after error or success:
onSettled: () => {
queryClient.invalidateQueries(["auth-user"]);
}
const {
data: user,
isLoading: isLoadingUser,
isFetching,
error: userError,
} = useFirestoreDocumentData(
["auth-user"],
doc(db, "users", userId),
{
subscribe: true,
}
);
@cyrus25 I've got the same problem with the manual update by invalidation as usually with react-query BUT here seems like it possible simply subscribe to updates
useFirestoreQueryData(['poopa', 'loopa'], ref, { subscribe: true })
Works perfectly for me so far
Thanks @vasyan , it works 👍