Open History to see the last commit
1---
2id: injectMutationState
3title: injectMutationState
4---
5β
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7β
8```ts
9function injectMutationState<TResult = MutationState<unknown, Error, unknown, unknown>>(injectMutationStateFn?: () => MutationStateOptions<TResult>, options?: InjectMutationStateOptions): Signal<TResult[]>;
10```
11β
12Defined in: [packages/angular-query-experimental/src/inject-mutation-state.ts:114](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-mutation-state.ts#L114)
13β
14Injects a signal that gives you access to all mutations in the `MutationCache`. You can pass `filters`
15([MutationFilters](../interfaces/MutationFilters.md)) to narrow down your mutations, and `select` to transform the mutation state.
16β
17## Type Parameters
18β
19### TResult
20β
21`TResult` = [`MutationState`](../interfaces/MutationState.md)\<`unknown`, `Error`, `unknown`, `unknown`\>
22β
23## Parameters
24β
25### injectMutationStateFn?
26β
27() => `MutationStateOptions`\<`TResult`\>
28β
29A function returning the `filters` to narrow down matched mutations, and an
30optional `select` to transform the mutation state. Similar to `computed` from Angular, this function runs
31in the reactive context, so signals read inside it re-narrow the matched mutations.
32β
33### options?
34β
35[`InjectMutationStateOptions`](../interfaces/InjectMutationStateOptions.md)
36β
37Additional configuration
38β
39<a id="options-properties"></a>
40β
41#### `options` properties
42β
43| Property | Type | Description |
44| ------ | ------ | ------ |
45| <a id="options-property-injector"></a> `injector?` | `Injector` | The `Injector` in which to create the mutation state signal. If this is not provided, the current injection context will be used instead (via `inject`). |
46β
47## Returns
48β
49`Signal`\<`TResult`[]\>
50β
51A `Signal` with an Array of whatever `select` returns for each matching mutation.
52β
53## Examples
54β
55Get all variables of all running mutations:
56```angular-ts
57@Component({
58 selector: 'pending-posts',
59 template: `{{ pendingVariables().length }} posts saving...`,
60})
61export class PendingPosts {
62 readonly pendingVariables = injectMutationState(() => ({
63 filters: { status: 'pending' },
64 select: (mutation) => mutation.state.variables,
65 }))
66}
67```
68β
69Get all data for specific mutations via the `mutationKey`:
70```angular-ts
71const mutationKey = ['posts']
72β
73@Component({
74 selector: 'posts',
75 template: `
76 <button (click)="createPost()">
77 Create post ({{ savedPosts().length }} saved so far)
78 </button>
79 `,
80})
81export class Posts {
82 // Some mutation that we want to get the state for
83 readonly createPostMutation = injectMutation(() => ({
84 mutationKey,
85 mutationFn: createPosts,
86 }))
87β
88 readonly savedPosts = injectMutationState(() => ({
89 // this mutation key needs to match the mutation key of the given mutation (see above)
90 filters: { mutationKey, status: 'success' },
91 select: (mutation) => mutation.state.data,
92 }))
93β
94 createPost() {
95 this.createPostMutation.mutate(['New Post'])
96 }
97}
98```