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

injectIsFetching.md

87 lines3.0 KB

1---
2id: injectIsFetching
3title: injectIsFetching
4---
5​
6```ts
7function injectIsFetching(filters?: QueryFilters<readonly unknown[]>, options?: InjectIsFetchingOptions): Signal<number>;
8```
9​
10Defined in: [packages/angular-query-experimental/src/inject-is-fetching.ts:63](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-is-fetching.ts#L63)
11​
12Injects a signal that tracks the number of queries that your application is loading or fetching in the
13background (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### options?
37​
38[`InjectIsFetchingOptions`](../interfaces/InjectIsFetchingOptions.md)
39​
40Additional configuration
41​
42<a id="options-properties"></a>
43​
44#### `options` properties
45​
46| Property | Type | Description |
47| ------ | ------ | ------ |
48| <a id="options-property-injector"></a> `injector?` | `Injector` | The `Injector` in which to create the isFetching signal. If this is not provided, the current injection context will be used instead (via `inject`). |
49​
50## Returns
51​
52`Signal`\<`number`\>
53​
54A `Signal` with the number of queries that your application is currently loading or fetching in
55the background.
56​
57## Examples
58​
59```angular-ts
60@Component({
61 selector: 'posts-fetching-indicator',
62 template: `
63 @if (isFetchingPosts()) {
64 <span>Refreshing posts...</span>
65 }
66 `,
67})
68export class PostsFetchingIndicator {
69 // How many queries matching the posts prefix are fetching?
70 readonly isFetchingPosts = injectIsFetching({ queryKey: ['posts'] })
72```
73​
74A global loading indicator for any query fetching in the background, not just the ones on screen:
75```angular-ts
76@Component({
77 selector: 'global-loading-indicator',
78 template: `
79 @if (isFetching()) {
80 <div>Queries are fetching in the background...</div>
81 }
82 `,
83})
84export class GlobalLoadingIndicator {
85 readonly isFetching = injectIsFetching()
87```