Mutations
client.mutation turns an endpoint into a useMutation hook. On top of TanStack's options it adds three settings of its own: invalidates, optimistic and useContext. This page covers the hook, its callbacks and how to tell which mutations are running.
The hook
The mutation is a hook that takes no arguments. mutate and mutateAsync take the endpoint's arguments.
The hook returns TanStack's useMutation result. data is the endpoint's validated response and error its EndpointError. When the endpoint requires no arguments, mutate() can be called without any.
Any endpoint works, including ones without a response body (response: 'void'), uploads and downloads. See Uploads and downloads for the last two.
Settings
Settings go in the second argument, or beside the endpoint options in an inline declaration. Every useMutation option except mutationKey and mutationFn is accepted and passed through: retry, scope, gcTime, meta, networkMode and so on.
The settings this package adds or changes are described below. optimistic has its own page.
Invalidating queries
invalidates lists what to refetch after the mutation succeeds. Each item is a query, meaning every cache entry of that endpoint, or a function that returns one key.
The function receives the mutation's data and variables. Return undefined from it to skip. The invalidations are awaited together with your onSuccess, so the mutation stays pending until the refetches of mounted queries have finished.
Callbacks and their context
onMutate, onSuccess, onError and onSettled run for every call of the mutation. Their last argument is a context object that already holds TanStack's client (the QueryClient), meta and mutationKey, so writing to the cache needs no extra hook:
In the last three, context.onMutateResult is whatever onMutate returned, or undefined if it did not run to completion.
Adding hooks with useContext
Callbacks are defined outside a component, so they cannot call hooks. useContext can: it runs inside the mutation hook on every render, and its return value is merged into the context of every callback.
TanStack's own fields win on a name clash, so do not return a property called client, meta or mutationKey.
Callbacks passed to mutate(variables, { onSuccess }) at the call site still work and run after these. Put what must always follow the mutation in the settings, and what belongs to one screen at the call site.
Status and headers
withResponse: true makes the mutation's data { data, response }, in the hook result and in onSuccess / onSettled. It is the same option queries have.
Which mutations are running
Every use of a mutation shares one key, [method, url], available as mutationKey. useIsMutating reports whether any call with that key is in flight, from any component:
With urlParams, only calls whose variables have those URL parameter values count. You may pass a subset of the parameters; values are compared as strings, so 42 matches '42'.
useUpdateUser.mutationKey is ['PATCH', '/users/$userId']. Pass it to TanStack's useMutationState or queryClient.isMutating for anything useIsMutating does not cover. keyPrefix and keySuffix apply to it as they do to query keys.
Related: Optimistic updates, Forms.