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

injectMutationState.md

98 lines2.9 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>>(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 }))
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 }
98```