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

useIsFetching.md

71 lines1.9 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?: Omit<QueryFilters<readonly unknown[]>, "fetchStatus">, queryClient?: QueryClient): number;
12```
13​
14Defined in: [packages/react-query/src/useIsFetching.ts:40](https://github.com/TanStack/query/blob/main/packages/react-query/src/useIsFetching.ts#L40)
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`Omit`\<[`QueryFilters`](../interfaces/QueryFilters.md)\<readonly `unknown`[]\>, `"fetchStatus"`\>
24​
25The [QueryFilters](../interfaces/QueryFilters.md) to narrow down the matched queries.
26​
27<a id="filters-properties"></a>
28​
29#### `filters` properties
30​
31Built from [`QueryFilters`](../interfaces/QueryFilters.md#properties). See the type above for what it changes.
32​
33### queryClient?
34​
35[`QueryClient`](../classes/QueryClient.md)
36​
37Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
38be used.
39​
40## Returns
41​
42`number`
43​
44Will be the `number` of the queries that your application is currently loading or fetching in the
45background.
46​
47## Examples
48​
49```tsx
50import { useIsFetching } from '@tanstack/react-query'
51​
52function PostsFetchingIndicator() {
53 // How many queries matching the posts prefix are fetching?
54 const isFetchingPosts = useIsFetching({ queryKey: ['posts'] })
55​
56 return isFetchingPosts ? <span>Refreshing posts...</span> : null
58```
59​
60A global loading indicator for any query fetching in the background, not just the ones on screen:
61```tsx
62import { useIsFetching } from '@tanstack/react-query'
63​
64function GlobalLoadingIndicator() {
65 const isFetching = useIsFetching()
66​
67 return isFetching ? (
68 <div>Queries are fetching in the background...</div>
69 ) : null
71```