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

mutationOptions.ts

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