T
TanStack3y ago
ambitious-aqua

queryClient.invalidateQueries works... but only for a while

Hi, I have the following. I want to refetch the list of favourites from my API every time a user favourites or unfavourites an item. This code works as expected, the app will send a POST or DELETE request, invalidate the queries, and make a GET request to refresh the favourites. But after a short amount of time, say ten minutes or so, it stops working. It will send the POST or DELETE, but the queries do not get invalidated to the favourites are never refreshed
export function useFavourite(options: UseMutationOptions = {}) {
const queryClient = useQueryClient();

return useMutation({
mutationFn: (data) => {
return setFavourite(data.uuid, data.remove)
},
onSuccess: () => {
queryClient.invalidateQueries({queryKey: ['favourites'], refetchType: 'all'});
},
...options
});
};
export function useFavourite(options: UseMutationOptions = {}) {
const queryClient = useQueryClient();

return useMutation({
mutationFn: (data) => {
return setFavourite(data.uuid, data.remove)
},
onSuccess: () => {
queryClient.invalidateQueries({queryKey: ['favourites'], refetchType: 'all'});
},
...options
});
};
5 Replies
ambitious-aqua
ambitious-aquaOP3y ago
Also worth noting my API logs show the POST/DELETE requests were successful, so onSuccess should run, and the queries should be invalidated And I've just determined onSuccess is called, so the issue is the QueryClient not invalidating the queries
fascinating-indigo
fascinating-indigo3y ago
Seems like the query is not actively used and thus garbage collected. The devtools would show them as inactive in that case.
ambitious-aqua
ambitious-aquaOP3y ago
Thanks, I'll keep an eye out in Flipper although it hasn't happened again since posting this If that's the case is there a way I can prevent it?
fascinating-indigo
fascinating-indigo3y ago
a query is used when a component is mounted and that component has a useQuery call
ambitious-aqua
ambitious-aquaOP3y ago
So it's started happening again and all the queries are showing as active in flipper

Did you find this page helpful?