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

useIsFetching.md

76 lines2.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:41](https://github.com/TanStack/query/blob/main/packages/react-query/src/useIsFetching.ts#L41)
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<a id="filters-properties"></a>
26​
27#### `filters` properties
28​
29| Property | Type | Default value | Description |
30| ------ | ------ | ------ | ------ |
31| <a id="filters-property-exact"></a> `exact?` | `boolean` | `undefined` | Match query key exactly |
32| <a id="filters-property-fetchstatus"></a> `fetchStatus?` | `"fetching"` \| `"paused"` \| `"idle"` | `undefined` | Include queries matching their fetchStatus |
33| <a id="filters-property-predicate"></a> `predicate?` | (`query`: [`Query`](../classes/Query.md)) => `boolean` | `undefined` | Include queries matching this predicate function |
34| <a id="filters-property-querykey"></a> `queryKey?` | `TQueryKey` \| `TuplePrefixes`\<`TQueryKey`\> | `undefined` | Include queries matching this query key |
35| <a id="filters-property-stale"></a> `stale?` | `boolean` | `undefined` | Include or exclude stale queries |
36| <a id="filters-property-type"></a> `type?` | `QueryTypeFilter` | `'all'` | Filter to active queries, inactive queries or all queries |
37​
38### queryClient?
39​
40[`QueryClient`](../classes/QueryClient.md)
41​
42Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
43be used.
44​
45## Returns
46​
47`number`
48​
49Will be the `number` of the queries that your application is currently loading or fetching in the
50background.
51​
52## Examples
53​
54```tsx
55import { useIsFetching } from '@tanstack/react-query'
56​
57function PostsFetchingIndicator() {
58 // How many queries matching the posts prefix are fetching?
59 const isFetchingPosts = useIsFetching({ queryKey: ['posts'] })
60​
61 return isFetchingPosts ? <span>Refreshing posts...</span> : null
63```
64​
65A global loading indicator for any query fetching in the background, not just the ones on screen:
66```tsx
67import { useIsFetching } from '@tanstack/react-query'
68​
69function GlobalLoadingIndicator() {
70 const isFetching = useIsFetching()
71​
72 return isFetching ? (
73 <div>Queries are fetching in the background...</div>
74 ) : null
76```