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

useMutationState.md

75 lines1.8 KB

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