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

mutationOptions.ts

103 lines3.2 KB

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