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

useMutationState.md

85 lines2.5 KB

1---
2id: useMutationState
3title: useMutationState
4---
5​
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7​
8```ts
9function useMutationState<TResult = MutationState<unknown, unknown, unknown, unknown>>(
10 host: ReactiveControllerHost,
11 options?: MutationStateOptions<TResult>,
12 queryClient?: QueryClient
13): MutationStateAccessor<TResult>;
14```
15​
16Defined in: [packages/lit-query/src/useMutationState.ts:186](https://github.com/TanStack/query/blob/main/packages/lit-query/src/useMutationState.ts#L186)
17​
18Creates a Lit reactive controller that selects state from matching mutations
19in the mutation cache.
20​
21When `options.filters` is a function, it is re-read during host updates so
22the selection can follow reactive host state. If `queryClient` is omitted,
23the controller resolves the client from the nearest connected
24`QueryClientProvider`.
25​
26## Type Parameters
27​
28### TResult
29​
30`TResult` = [`MutationState`](../interfaces/MutationState.md)\<`unknown`, `unknown`, `unknown`, `unknown`\>
31​
32## Parameters
33​
34### host
35​
36`ReactiveControllerHost`
37​
38The Lit reactive controller host that owns the mutation cache
39subscription.
40​
41### options?
42​
43[`MutationStateOptions`](../type-aliases/MutationStateOptions.md)\<`TResult`\> = `{}`
44​
45Mutation state filters and optional selector.
46​
47<a id="options-properties"></a>
48​
49#### `options` properties
50​
51| Property | Type | Description |
52| ------ | ------ | ------ |
53| <a id="options-property-filters"></a> `filters?` | [`Accessor`](../type-aliases/Accessor.md)\<[`MutationFilters`](../interfaces/MutationFilters.md)\> | Filters used to select mutations from the mutation cache. |
54| <a id="options-property-select"></a> `select?` | (`mutation`: [`Mutation`](../classes/Mutation.md)) => `TResult` | Maps each matching mutation to the value returned by the accessor. |
55​
56### queryClient?
57​
58[`QueryClient`](../classes/QueryClient.md)
59​
60Optional explicit query client. Provide this for
61controllers that should not resolve a client from Lit context.
62​
63## Returns
64​
65[`MutationStateAccessor`](../type-aliases/MutationStateAccessor.md)\<`TResult`\>
66​
67An accessor for the selected mutation state array.
68​
69## Example
70​
71```ts
72import { LitElement, html } from 'lit'
73import { useMutationState } from '@tanstack/lit-query'
74​
75class PendingUploads extends LitElement {
76 private readonly uploads = useMutationState(this, {
77 filters: { mutationKey: ['upload'], status: 'pending' },
78 select: (mutation) => mutation.state.variables as File,
79 })
80​
81 render() {
82 return html`<span>${this.uploads().length} uploads pending</span>`
83 }
85```