0
Fork 0
mirror of https://github.com/TanStack/query.git synced 2026-11-18 19:33:22 +00:00
query/docs/framework/lit/reference/functions/createMutationController.md
Wonsuk Choi f38e6271b4
chore(*): update '@tanstack/typedoc-config' to v0.3.5 (#12035)
* chore(scripts/generate-docs): read type alias bodies whose type parameters have defaults

* chore(*): update '@tanstack/typedoc-config' to v0.3.5
2026-10-17 11:14:03 +09:00

2.7 KiB

id title
createMutationController createMutationController
function createMutationController<TData = unknown, TError = Error, TVariables = void, TOnMutateResult = unknown>(
   host: ReactiveControllerHost,
   options: Accessor<CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>>,
   queryClient?: QueryClient
): MutationResultAccessor<TData, TError, TVariables, TOnMutateResult>;

Defined in: packages/lit-query/src/createMutationController.ts:326

Creates a Lit reactive controller that subscribes the host to a mutation.

The returned accessor is callable and also exposes current, mutate, mutateAsync, reset, and destroy. When options is a function, it is re-read during host updates so mutation options can follow reactive host state.

If queryClient is omitted, the controller resolves the client from the nearest connected QueryClientProvider.

Type Parameters

TData

TData = unknown

TError

TError = Error

TVariables

TVariables = void

TOnMutateResult

TOnMutateResult = unknown

Parameters

host

ReactiveControllerHost

The Lit reactive controller host that owns the mutation subscription.

options

Accessor<CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>>

Mutation observer options, or a getter that returns options.

options properties

Built from MutationObserverOptions. See the type above for what it changes.

queryClient?

QueryClient

Optional explicit query client. Provide this for controllers that should not resolve a client from Lit context.

Returns

MutationResultAccessor<TData, TError, TVariables, TOnMutateResult>

An accessor for the latest mutation result with mutation helper methods.

Example

import { LitElement, html } from 'lit'
import { createMutationController } from '@tanstack/lit-query'

class AddTodoForm extends LitElement {
  private readonly addTodo = createMutationController(this, {
    mutationFn: (title: string) =>
      fetch('/api/todos', { method: 'POST', body: JSON.stringify({ title }) }),
  })

  render() {
    const mutation = this.addTodo()

    return html`
      <button ?disabled=${mutation.isPending} @click=${() => this.addTodo.mutate('Ship docs')}>
        Add todo
      </button>
    `
  }
}