Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useIsFetching.md

63 lines1.6 KB

1---
2id: useIsFetching
3title: useIsFetching
4redirect_from:
5 - framework/react/reference/useIsFetching
6---
7​
8```ts
9function useIsFetching(filters?: QueryFilters<readonly unknown[]>, queryClient?: QueryClient): number;
10```
11​
12Defined in: [packages/react-query/src/useIsFetching.ts:44](https://github.com/TanStack/query/blob/main/packages/react-query/src/useIsFetching.ts#L44)
13​
14The `useIsFetching` hook returns the `number` of the queries that your application is loading or fetching in
15the background (useful for app-wide loading indicators).
16​
17## Parameters
18​
19### filters?
20​
21[`QueryFilters`](../interfaces/QueryFilters.md)\<readonly `unknown`[]\>
22​
23The [QueryFilters](../interfaces/QueryFilters.md) to narrow down the matched queries.
24​
25### queryClient?
26​
27[`QueryClient`](../classes/QueryClient.md)
28​
29Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
30be used.
31​
32## Returns
33​
34`number`
35​
36Will be the `number` of the queries that your application is currently loading or fetching in the
37background.
38​
39## Examples
40​
41```tsx
42import { useIsFetching } from '@tanstack/react-query'
43​
44function PostsFetchingIndicator() {
45 // How many queries matching the posts prefix are fetching?
46 const isFetchingPosts = useIsFetching({ queryKey: ['posts'] })
47​
48 return isFetchingPosts ? <span>Refreshing posts...</span> : null
50```
51​
52A global loading indicator for any query fetching in the background, not just the ones on screen:
53```tsx
54import { useIsFetching } from '@tanstack/react-query'
55​
56function GlobalLoadingIndicator() {
57 const isFetching = useIsFetching()
58​
59 return isFetching ? (
60 <div>Queries are fetching in the background...</div>
61 ) : null
63```