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

mutationOptions.ts

93 lines3.1 KB

1import type { DefaultError, WithRequired } from '@tanstack/query-core'
2​
3import type { UseMutationOptions } from './types'
4​
5/**
6 * You can generally pass everything to `mutationOptions` that you can also pass to `useMutation`. A
7 * `mutationKey` is required on this overload so the mutation can be looked up later, e.g. with
8 * `useMutationState`.
9 * @param options - The mutation options to use, identical to what you'd pass to `useMutation`, with a
10 * required `mutationKey`.
11 * @returns The same options object, unchanged.
12 * @see {@link useMutation} to run the mutation these options describe.
13 * @example
14 * Looking the mutation up elsewhere via its `mutationKey`, e.g. for a global "saving…" indicator:
15 * ```tsx
16 * import { mutationOptions, useMutationState } from '@tanstack/preact-query'
17 *
18 * const createPostOptions = mutationOptions({
19 * mutationKey: ['posts', 'create'],
20 * mutationFn: createPost,
21 * })
22 *
23 * function SavingIndicator() {
24 * const isCreatingPost = useMutationState({
25 * filters: { mutationKey: createPostOptions.mutationKey, status: 'pending' },
26 * }).length > 0
27 *
28 * return isCreatingPost ? <span>Saving…</span> : null
29 * }
30 * ```
31 */
32export function mutationOptions<
33 TData = unknown,
34 TError = DefaultError,
35 TVariables = void,
36 TOnMutateResult = unknown,
37>(
38 options: WithRequired<
39 UseMutationOptions<TData, TError, TVariables, TOnMutateResult>,
40 'mutationKey'
41 >,
42): WithRequired<
43 UseMutationOptions<TData, TError, TVariables, TOnMutateResult>,
44 'mutationKey'
46/**
47 * You can generally pass everything to `mutationOptions` that you can also pass to `useMutation`. No
48 * `mutationKey` is required on this overload — use this when you don't need to target the mutation via a
49 * `mutationKey` filter later (e.g. with `useMutationState`); it can still be observed through other filters,
50 * such as `status`.
51 * @param options - The mutation options to use, identical to what you'd pass to `useMutation`, without a
52 * `mutationKey`.
53 * @returns The same options object, unchanged.
54 * @remarks See the other overload's example for looking a mutation up via `useMutationState`.
55 * @see {@link useMutation} to run the mutation these options describe.
56 * @example
57 * ```tsx
58 * import { mutationOptions, useMutation } from '@tanstack/preact-query'
59 *
60 * export const createPostOptions = mutationOptions({
61 * mutationFn: createPost,
62 * })
63 *
64 * function CreatePost() {
65 * const mutation = useMutation(createPostOptions)
66 * return <button onClick={() => mutation.mutate({ title: 'Hello' })}>Create</button>
67 * }
68 * ```
69 */
70export function mutationOptions<
71 TData = unknown,
72 TError = DefaultError,
73 TVariables = void,
74 TOnMutateResult = unknown,
75>(
76 options: Omit<
77 UseMutationOptions<TData, TError, TVariables, TOnMutateResult>,
78 'mutationKey'
79 >,
80): Omit<
81 UseMutationOptions<TData, TError, TVariables, TOnMutateResult>,
82 'mutationKey'
84export function mutationOptions<
85 TData = unknown,
86 TError = DefaultError,
87 TVariables = void,
88 TOnMutateResult = unknown,
89>(
90 options: UseMutationOptions<TData, TError, TVariables, TOnMutateResult>,
91): UseMutationOptions<TData, TError, TVariables, TOnMutateResult> {
92 return options