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

useIsFetching.md

69 lines1.9 KB

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