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