TanStackT
TanStack15mo ago
3 replies
wet-aqua

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);
},
});
};
Was this page helpful?