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

injectMutationState.md

96 lines2.8 KB

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:114](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-mutation-state.ts#L114)
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<a id="options-properties"></a>
38​
39#### `options` properties
40​
41| Property | Type | Description |
42| ------ | ------ | ------ |
43| <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`). |
44​
45## Returns
46​
47`Signal`\<`TResult`[]\>
48​
49A `Signal` with an Array of whatever `select` returns for each matching mutation.
50​
51## Examples
52​
53Get all variables of all running mutations:
54```angular-ts
55@Component({
56 selector: 'pending-posts',
57 template: `{{ pendingVariables().length }} posts saving...`,
58})
59export class PendingPosts {
60 readonly pendingVariables = injectMutationState(() => ({
61 filters: { status: 'pending' },
62 select: (mutation) => mutation.state.variables,
63 }))
65```
66​
67Get all data for specific mutations via the `mutationKey`:
68```angular-ts
69const mutationKey = ['posts']
70​
71@Component({
72 selector: 'posts',
73 template: `
74 <button (click)="createPost()">
75 Create post ({{ savedPosts().length }} saved so far)
76 </button>
77 `,
78})
79export class Posts {
80 // Some mutation that we want to get the state for
81 readonly createPostMutation = injectMutation(() => ({
82 mutationKey,
83 mutationFn: createPosts,
84 }))
85​
86 readonly savedPosts = injectMutationState(() => ({
87 // this mutation key needs to match the mutation key of the given mutation (see above)
88 filters: { mutationKey, status: 'success' },
89 select: (mutation) => mutation.state.data,
90 }))
91​
92 createPost() {
93 this.createPostMutation.mutate(['New Post'])
94 }
96```