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