Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useMutation.md

92 lines3.3 KB

function useMutation<TData = unknown, TError = Error, TVariables = void, TOnMutateResult = unknown>(options: UseMutationOptions<TData, TError, TVariables, TOnMutateResult>, queryClient?: Accessor<QueryClient>): UseMutationResult<TData, TError, TVariables, TOnMutateResult>;

Defined in: packages/solid-query/src/useMutation.ts:99

Mutations ride core action: each mutate() call is one transaction.

  • Transient flight state (isPending, in-flight variables) is a createOptimistic overlay written at the top of the action β€” visible immediately, dropped automatically when the transition settles. The engine's revert-on-settle IS the submission state machine.
  • options.onMutate(variables) runs inside the same window: it is the place to apply the caller's own optimistic overlays (createOptimistic / createOptimisticStore writes). No context object, no rollback plumbing β€” reverting is the engine's job.
  • The fetch itself goes through query-core: mutationCache.build().execute() supplies retry/backoff, offline pausing, scoped serial execution, and cache-level lifecycle callbacks. No MutationObserver β€” the adapter reads the Mutation's state directly where flight metadata (failureCount, isPaused) is needed.
  • Options-level callbacks are stripped from the built mutation and re-run post-yield, inside the transaction: an onSuccess β†’ invalidateQueries chain issues version bumps whose refetches hold this very transition, so mutation success and fresh query data land in one atomic paint (and a single-flight payload has already primed the cache by the time execute resolves).
  • One mutate, returning a safe-to-ignore promise: rejection is also routed into reactive state, and a no-op catch keeps the ignored branch from surfacing as an unhandled rejection. The mutate/mutateAsync split existed for React's callback-vs-promise error routing; it has no space here.

Type Parameters

TData

TData = unknown

TError

TError = Error

TVariables

TVariables = void

TOnMutateResult

TOnMutateResult = unknown

Parameters

options

UseMutationOptions<TData, TError, TVariables, TOnMutateResult>

An accessor returning the UseMutationOptions to use.

options properties

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

queryClient?

Accessor<QueryClient>

An accessor for a custom QueryClient. Otherwise, the one from the nearest context will be used.

Returns

UseMutationResult<TData, TError, TVariables, TOnMutateResult>

The mutation state, with mutate to run the mutation and reset to clear its state.

Result properties

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