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