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

injectMutationState.md

97 lines3.1 KB

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 }))
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 }
97```