function mutationOptions<TData, TError, TVariables, TOnMutateResult>(options): WithRequired<UseMutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">;Defined in: preact-query/src/mutationOptions.ts:49
You can generally pass everything to mutationOptions that you can also pass to useMutation. A mutationKey is required on this overload so the mutation can be looked up later, e.g. with useMutationState.
TData = unknown
TError = Error
TVariables = void
TOnMutateResult = unknown
WithRequired<UseMutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">
The mutation options to use, identical to what you'd pass to useMutation, with a required mutationKey.
WithRequired<UseMutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">
The same options object, unchanged.
useMutation to run the mutation these options describe.
import { mutationOptions, useMutation } from '@tanstack/preact-query'
export const createPostOptions = mutationOptions({
mutationKey: ['posts', 'create'],
mutationFn: createPost,
})
function CreatePost() {
const mutation = useMutation(createPostOptions)
return <button onClick={() => mutation.mutate({ title: 'Hello' })}>Create</button>
}Looking the mutation up elsewhere via its mutationKey, e.g. for a global "saving…" indicator:
import { mutationOptions, useMutationState } from '@tanstack/preact-query'
const createPostOptions = mutationOptions({
mutationKey: ['posts', 'create'],
mutationFn: createPost,
})
function SavingIndicator() {
const isCreatingPost = useMutationState({
filters: { mutationKey: createPostOptions.mutationKey, status: 'pending' },
}).length > 0
return isCreatingPost ? <span>Saving…</span> : null
}function mutationOptions<TData, TError, TVariables, TOnMutateResult>(options): Omit<UseMutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">;Defined in: preact-query/src/mutationOptions.ts:87
You can generally pass everything to mutationOptions that you can also pass to useMutation. No mutationKey is required on this overload — use this when you don't need to look the mutation up later (e.g. with useMutationState).
TData = unknown
TError = Error
TVariables = void
TOnMutateResult = unknown
Omit<UseMutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">
The mutation options to use, identical to what you'd pass to useMutation, without a mutationKey.
Omit<UseMutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">
The same options object, unchanged.
useMutation to run the mutation these options describe.
import { mutationOptions, useMutation } from '@tanstack/preact-query'
export const createPostOptions = mutationOptions({
mutationFn: createPost,
})
function CreatePost() {
const mutation = useMutation(createPostOptions)
return <button onClick={() => mutation.mutate({ title: 'Hello' })}>Create</button>
}