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

useMutationState.md

73 lines1.7 KB

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