Skip to content
Source
Sign inSign up
TanStack/query

useIsFetching always returns false #3437

Send to my agent
ClosedTkDodo opened this issue 8 comments
TkDodocommented

Describe the bug

useIsFetching always shows zero when loading for the first time.

Your minimal, reproducible example

https://codesandbox.io/s/react-query-starting-point-forked-zz56e1?file=/src/App.tsx

Steps to reproduce

look at the sandbox isFetching should be true at some point, but it's not

note that it works if you change the order of Example and GlobalFetchingIndicator

Expected behavior

useIsFetching should show the correct fetching count

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

all of them

react-query version

v3 and v4

TypeScript version

No response

Additional context

it seems that subscription order is important. We check for isFetching when the hook mounts, and then again inside the subscription in the effect. However:

  • on mount, we are not (yet) fetching
  • when the effect runs and subscribes, we don't immediately receive an event, because the fetch was already triggered (useEffect execution order)
  • the next even we receive is when the query has finished loading, so we are again not fetching

A potential solution could be to check for queryClient.isFetching once again if the effect runs, but outside of the subscription.

  1. TkDodo added labelsbug

  2. TkDodo closed this

  3. :tada: This issue has been resolved in version 3.34.17 :tada:

    The release is available on:

    Your semantic-release bot :package::rocket:

  4. tannerlinsley added labelsreleased

  5. :tada: This issue has been resolved in version 4.0.0-alpha.22 :tada:

    The release is available on:

    Your semantic-release bot :package::rocket:

  6. tannerlinsley added labelsreleased on @alpha

  7. @TkDodo the 3.34.17 have fixed for you?

    I'm using the 3.38.0 and the useIsRetching always returns 0

  8. Yes, try out the sandbox with the latest version.

  9. @TkDodo

    Hmm, I discovered that always returns 0 when I pass more than one key like:

    queryClient.isFetching(['firstKey','secondKey'])

    so I think that is not supported or I'm doing something wrong

    Did you know if there's some way to watch for multiples specific queries?

  10. If you want to target multiple keys, you'd need to use the predicate function argument, or call the hook twice.

  11. If you want to target multiple keys, you'd need to use the predicate function argument, or call the hook twice.

    do you have a demo?

  12. @TkDodo

    Hmm, I discovered that always returns 0 when I pass more than one key like:

    queryClient.isFetching(['firstKey','secondKey'])

    so I think that is not supported or I'm doing something wrong

    Did you know if there's some way to watch for multiples specific queries?

    i have the same question

Sign in to join this conversation.