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

useIsFetching.md

78 lines2.7 KB

1---
2id: useIsFetching
3title: useIsFetching
4redirect_from:
5 - framework/react/reference/useIsFetching
6---
7​
8<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
9​
10```ts
11function useIsFetching(filters?: QueryFilters<readonly unknown[]>, queryClient?: QueryClient): number;
12```
13​
14Defined in: [packages/react-query/src/useIsFetching.ts:41](https://github.com/TanStack/query/blob/main/packages/react-query/src/useIsFetching.ts#L41)
15​
16The `useIsFetching` hook returns the `number` of the queries that your application is loading or fetching in
17the background (useful for app-wide loading indicators).
18​
19## Parameters
20​
21### filters?
22​
23[`QueryFilters`](../interfaces/QueryFilters.md)\<readonly `unknown`[]\>
24​
25The [QueryFilters](../interfaces/QueryFilters.md) to narrow down the matched queries.
26​
27<a id="filters-properties"></a>
28​
29#### `filters` properties
30​
31| Property | Type | Default value | Description |
32| ------ | ------ | ------ | ------ |
33| <a id="filters-property-exact"></a> `exact?` | `boolean` | `undefined` | Match query key exactly |
34| <a id="filters-property-fetchstatus"></a> `fetchStatus?` | `"fetching"` \| `"paused"` \| `"idle"` | `undefined` | Include queries matching their fetchStatus |
35| <a id="filters-property-predicate"></a> `predicate?` | (`query`: [`Query`](../classes/Query.md)) => `boolean` | `undefined` | Include queries matching this predicate function |
36| <a id="filters-property-querykey"></a> `queryKey?` | `TQueryKey` \| `TuplePrefixes`\<`TQueryKey`\> | `undefined` | Include queries matching this query key |
37| <a id="filters-property-stale"></a> `stale?` | `boolean` | `undefined` | Include or exclude stale queries |
38| <a id="filters-property-type"></a> `type?` | `QueryTypeFilter` | `'all'` | Filter to active queries, inactive queries or all queries |
39​
40### queryClient?
41​
42[`QueryClient`](../classes/QueryClient.md)
43​
44Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
45be used.
46​
47## Returns
48​
49`number`
50​
51Will be the `number` of the queries that your application is currently loading or fetching in the
52background.
53​
54## Examples
55​
56```tsx
57import { useIsFetching } from '@tanstack/react-query'
58​
59function PostsFetchingIndicator() {
60 // How many queries matching the posts prefix are fetching?
61 const isFetchingPosts = useIsFetching({ queryKey: ['posts'] })
62​
63 return isFetchingPosts ? <span>Refreshing posts...</span> : null
65```
66​
67A global loading indicator for any query fetching in the background, not just the ones on screen:
68```tsx
69import { useIsFetching } from '@tanstack/react-query'
70​
71function GlobalLoadingIndicator() {
72 const isFetching = useIsFetching()
73​
74 return isFetching ? (
75 <div>Queries are fetching in the background...</div>
76 ) : null
78```