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