Open History to see the last commit
1---
2id: createMutationController
3title: createMutationController
4---
5β
6# Function: createMutationController()
7β
8```ts
9function createMutationController<TData, TError, TVariables, TOnMutateResult>(
10 host,
11 options,
12queryClient?): MutationResultAccessor<TData, TError, TVariables, TOnMutateResult>;
13```
14β
15Defined in: [packages/lit-query/src/createMutationController.ts:338](https://github.com/TanStack/query/blob/main/packages/lit-query/src/createMutationController.ts#L338)
16β
17Creates a Lit reactive controller that subscribes the host to a mutation.
18β
19The returned accessor is callable and also exposes `current`, `mutate`,
20`mutateAsync`, `reset`, and `destroy`. When `options` is a function, it is
21re-read during host updates so mutation options can follow reactive host
22state.
23β
24If `queryClient` is omitted, the controller resolves the client from the
25nearest connected `QueryClientProvider`.
26β
27## Type Parameters
28β
29### TData
30β
31`TData` = `unknown`
32β
33### TError
34β
35`TError` = `Error`
36β
37### TVariables
38β
39`TVariables` = `void`
40β
41### TOnMutateResult
42β
43`TOnMutateResult` = `unknown`
44β
45## Parameters
46β
47### host
48β
49`ReactiveControllerHost`
50β
51The Lit reactive controller host that owns the mutation
52subscription.
53β
54### options
55β
56[`Accessor`](../type-aliases/Accessor.md)\<[`CreateMutationOptions`](../type-aliases/CreateMutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>\>
57β
58Mutation observer options, or a getter that returns options.
59β
60### queryClient?
61β
62`QueryClient`
63β
64Optional explicit query client. Provide this for
65controllers that should not resolve a client from Lit context.
66β
67## Returns
68β
69[`MutationResultAccessor`](../type-aliases/MutationResultAccessor.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
70β
71An accessor for the latest mutation result with mutation helper
72methods.
73β
74## Example
75β
76```ts
77import { LitElement, html } from 'lit'
78import { createMutationController } from '@tanstack/lit-query'
79β
80class AddTodoForm extends LitElement {
81 private readonly addTodo = createMutationController(this, {
82 mutationFn: (title: string) =>
83 fetch('/api/todos', { method: 'POST', body: JSON.stringify({ title }) }),
84 })
85β
86 render() {
87 const mutation = this.addTodo()
88β
89 return html`
90 <button ?disabled=${mutation.isPending} @click=${() => this.addTodo.mutate('Ship docs')}>
91 Add todo
92 </button>
93 `
94 }
95}
96```