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