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>, TMutation extends Mutation<any, any, any, any> = MutationTypeFromResult<TResult>>(options?: () => MutationStateOptions<TResult, TMutation>): Signal<TResult[]>;
10```
11β
12Defined in: [packages/angular-query/src/inject-mutation-state.ts:118](https://github.com/TanStack/query/blob/main/packages/angular-query/src/inject-mutation-state.ts#L118)
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### TMutation
24β
25`TMutation` *extends* [`Mutation`](../classes/Mutation.md)\<`any`, `any`, `any`, `any`\> = `MutationTypeFromResult`\<`TResult`\>
26β
27## Parameters
28β
29### options?
30β
31() => [`MutationStateOptions`](../type-aliases/MutationStateOptions.md)\<`TResult`, `TMutation`\>
32β
33A function returning the `filters` to narrow down matched mutations, and an optional
34`select` to transform the mutation state. Similar to `computed` from Angular, this function runs in the
35reactive context, so signals read inside it re-narrow the matched mutations.
36β
37<a id="options-properties"></a>
38β
39#### `options` properties
40β
41| Property | Type | Description |
42| ------ | ------ | ------ |
43| <a id="options-property-filters"></a> `filters?` | [`MutationFilters`](../interfaces/MutationFilters.md) | Restricts the mutation cache entries included in the result. |
44| <a id="options-property-select"></a> `select?` | (`mutation`: `TMutation`) => `TResult` | Maps each matching mutation to the value exposed in the result signal. |
45β
46## Returns
47β
48`Signal`\<`TResult`[]\>
49β
50A `Signal` with an Array of whatever `select` returns for each matching mutation.
51β
52## Examples
53β
54Get all variables of all running mutations:
55```angular-ts
56@Component({
57 selector: 'pending-posts',
58 template: `{{ pendingVariables().length }} posts saving...`,
59})
60export class PendingPosts {
61 readonly pendingVariables = injectMutationState(() => ({
62 filters: { status: 'pending' },
63 select: (mutation) => mutation.state.variables,
64 }))
65}
66```
67β
68Get all data for specific mutations via the `mutationKey`:
69```angular-ts
70const mutationKey = ['posts']
71β
72@Component({
73 selector: 'posts',
74 template: `
75 <button (click)="createPost()">
76 Create post ({{ savedPosts().length }} saved so far)
77 </button>
78 `,
79})
80export class Posts {
81 // Some mutation that we want to get the state for
82 readonly createPostMutation = injectMutation(() => ({
83 mutationKey,
84 mutationFn: createPosts,
85 }))
86β
87 readonly savedPosts = injectMutationState(() => ({
88 // this mutation key needs to match the mutation key of the given mutation (see above)
89 filters: { mutationKey, status: 'success' },
90 select: (mutation) => mutation.state.data,
91 }))
92β
93 createPost() {
94 this.createPostMutation.mutate(['New Post'])
95 }
96}
97```