Skip to main content
Perform mutations and side effects with the createMutation function. It provides methods to trigger mutations and tracks their state reactively in Svelte 5.

Signature

Parameters

Accessor<CreateMutationOptions<TData, TError, TVariables, TContext>>
required
A function (accessor) returning mutation configuration options.
Accessor<QueryClient>
Accessor returning a custom QueryClient instance. If not provided, uses the client from context.

Returns

object
Reactive mutation state and methods.

Type Parameters

  • TData - Type of data returned by the mutation
  • TError - Type of error (defaults to DefaultError)
  • TVariables - Type of variables passed to the mutation (defaults to void)
  • TContext - Type of context returned by onMutate (defaults to unknown)

Examples

Basic Usage (Svelte 5 Runes)

With TypeScript

Optimistic Updates

Using mutateAsync

Per-Mutation Callbacks

Reset Mutation State

Multiple Mutations

Error Handling with Retry

Reactive Mutation Options

Form Handling

Loading States

Notes

Svelte Query uses Svelte 5’s runes mode. The options parameter must be an accessor (function).
Use mutate for fire-and-forget operations and mutateAsync when you need to await the result or handle errors with try/catch.
Always invalidate or update relevant queries after a successful mutation to keep your UI in sync with the server state.