query: refetch does not respect enabled false

Describe the bug

~Is it possible that this (#1797) has regressed? I’m on ^3.12.1 and running into exactly this.~

When calling refetch, there does not seem to be a way to respect enabled. Manually refetching the query always goes through. This by itself is not an issue, but that means that the only way to use a refetch-but-respect-query-options is to:

  • use queryClient directly
  • copy the enabled logic and execute it before calling refetch

When the refetch is in flight, it will actually keep retrying the query too if the query fails. This can be overcome by copying the enabled logic once more to the retry function.

Expected behavior There is a way to refetchIfEnabled.


Perhaps this can be added to the optional options of refetch as a flag?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 20

Most upvoted comments

I have a use case where I would like to have a “refetchIfEnabled” too.

  1. My app sets “enabled: false” as default option until the user is authenticated.
  2. Once I have a valid token, I set the bearer token on Axios (which I use to fetch), and then I reset the default options.
  3. From that point, I instruct react query to start fetching data using queryClient.refetchQueries()

But as mentioned earlier, this causes ALL queries to be re-fetched, even the ones that are explicitly marked as disabled. I tried using queryClient.invalidateQueries() as suggested by @TkDodo, but this does not trigger a fetch as they’ve never been fetched before. An option if refetchQueries or a refetchIfEnabled method would be handy here.

just trying to make suggestions. Like I said, I would architect it differently:

const myQuery = () => {
  const token = useToken() // assuming this will get set and trigger a re-render once you have a token
  return useQuery(['myKey', token], () => fetchByToken(token), { enabled: !!token })
}