I’m building a dictionary component, where data is fetched every time user changes a search term, however, if he then changes it to a term that has been cached previously, I don’t need it refetched, obviously data won’t be changed. I have set both stateTime and gcTime to Infinity, but every time I come back to a previously fetched term, query refetches.
I couldn’t find an explanation for this, so I can only assume cache is cleared when query key changes? Is there a way around it?
export const useSearchDictionaryQuery = (lang: Languages, searchTerm: string) =>
useQuery({
queryKey: ['searchDictionary', searchTerm, lang],
queryFn: () => fetchSearchDictionary(lang, searchTerm),
enabled: false,
staleTime: Infinity,
gcTime: Infinity,
});
Edit: the cache is not actually cleared, if I go back to a previous term, is shows the result immediately, but also starts a background refetch, which is what I don’t need.
3