Force refetch #3078
-
Hi there, I'm trying to force refetch a query. What I need is after the query has loaded and cached, When the user clicks a button, I want to reload the same query with displaying a loading state. Here's the code example (forked from basic example) https://codesandbox.io/s/cranky-gagarin-mz0ho Basically, I'm using
If you look in the network tab, you can see that the request fires twice. What do I do wrong? Is there another propper way to force refetch a query? PS I figured out I can use isFetching along with isLoading, but two requests on remove + refetch seem like a bug. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
there is only one request ;)
alternatively, you could also use anyways, why you are seeing two requests is likely because of
--> but because you have the network tab focussed, your button click will trigger a refocus, which fetches, and then a refetch, which also refetches :) if you click anywhere else on the page first, and then click the retry button, you can see that each click will only fire one request. or you can disable refetchOnWindowFocus - it's a common "trap" during dev-mode |
Beta Was this translation helpful? Give feedback.
there is only one request ;)
remove
only removes the query from the cache and doesn't inform observers.refetch
then refetches it, giving you a cleanloading
statealternatively, you could also use
queryClient.resetQueries(key)
. It will reset the query to its initial state and refetch. I have an open PR to add areset
method to return it from useQuery, but I haven't really seen enough use-cases yet to actually add it. Maybe this is it?anyways, why you are seeing two requests is likely because of
refetchOnWindowFocus
:--> but because you have the net…