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

useIsFetching.md

74 lines2.5 KB

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