T
TanStack16mo ago
like-gold

setQueryData + refetch vs fetchQuery

I'm reviewing a PR that has setQueryData + refetch (page uses useInfiniteQuery)
queryClient.setQueryData(queryKey, (data: any) => ({
...data,
pageParams: [now],
}));

refetch();
queryClient.setQueryData(queryKey, (data: any) => ({
...data,
pageParams: [now],
}));

refetch();
Wondering if its best practice to just use useInfiniteQuery with the new params or fetchInfiniteQuery?
10 Replies
vicious-gold
vicious-gold16mo ago
what are you trying to achieve ?
like-gold
like-goldOP16mo ago
its a refetch button on an app (webview) that performs a refetch. My understanding is we should just change the params to useInfiniteQuery or use fetchInfiniteQuery. This PR has setQueryData which is updating the pageParams with a date of now. The api returns a list based on a date (now)
vicious-gold
vicious-gold16mo ago
refetch is meant for refetch with the same params
like-gold
like-goldOP16mo ago
but what about in the instance above where a setQueryData and refetch is happning on a button click
vicious-gold
vicious-gold16mo ago
if it works it works. I've never done that
like-gold
like-goldOP16mo ago
cool.Can you clarify if running the query again will do the same thing? Is there any advantage to using setQuery data?
vicious-gold
vicious-gold16mo ago
It won't have the right pageParam, I guess that's why you're doing it
like-gold
like-goldOP16mo ago
what i mean is change those params. So change those pageParams and it will perform a query again. Seems a better way of doing this than setQuery + refetch?
vicious-gold
vicious-gold16mo ago
how would you change the pageParam ?
like-gold
like-goldOP16mo ago
ah yes of course. 🤦‍♂️ its not state its passed in

Did you find this page helpful?