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