Issue with data persisting
Little background :
So I am creating an app that calls 5+ APIs for the first load.
Also to update any of this data, i make an API call to backend and save the response in the queryKeys with setQueryData()
Issue
adding staleTime = Infinity makes the data stale and not reload on every page change (exactly what i want)
but the setQueryData updates goes back to the first loaded data
adding gcTime = 1hr makes the setQueryData updates persists on page changes.
but the the data reloads on every page change.
I do not want the api data to refetch on any page change and also persist the setQueryData updates on any page change.
current queryclient -
const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
staleTime: 1000 * 60 * 60 * 1, // 1 hour // this works with constant refreseshs but not with queryData updates
gcTime: 1000 * 60 * 60 * 1, // 1 hour // this works with queryData updates but not with constant refreseshs
}}});
one of the setQueryData
export const useUpdateNote = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: updateNote,
onSuccess: ({ error, data }) => {
if (error) {
errorToast(
${error}
);
return;
}
if (!data) {
errorToast("No data returned");
return;
}
queryClient.setQueryData(["notes", data.id], data);
},
});
};3 Replies
hilarious-sapphire•10mo ago
I think you have the right initial setup. Infinite stale time will prevent refetching. and setQueryData still updates that data and subscribers.
I'm confused about what you're seeing though.
gcTime is garbage collection. If data has no observers for x time, it's removed from cache.
I could be wrong, but I think if data is garbage collected, it will still refetch when you revist a page that has an infinite stale time query.
In your current queryClient, the staleTime is 1 hour. Not infinity.
afraid-scarletOP•10mo ago
the gc time isn't much of a concern.
the main issue is staletime -
and i want both the api data not refreshing constantly andd the querydata persisting solution.
hilarious-sapphire•10mo ago
I think you have the right initial setup. Infinite stale time will prevent refetching. and setQueryData still updates that data and subscribers.
I'm confused about what you're seeing though.I think you need a repro.