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

mutationOptions.md

144 lines4.0 KB

1---
2id: mutationOptions
3title: mutationOptions
4redirect_from:
5 - framework/react/reference/mutationOptions
6---
7​
8## Call Signature
9​
10```ts
11function mutationOptions<TData, TError, TVariables, TOnMutateResult>(options: WithRequired<UseMutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">): WithRequired<UseMutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">;
12```
13​
14Defined in: [packages/react-query/src/mutationOptions.ts:33](https://github.com/TanStack/query/blob/main/packages/react-query/src/mutationOptions.ts#L33)
15​
16You can generally pass everything to `mutationOptions` that you can also pass to `useMutation`. A
17`mutationKey` is required on this overload so the mutation can be looked up later, e.g. with
18`useMutationState`.
19​
20### Type Parameters
21​
22#### TData
23​
24`TData` = `unknown`
25​
26#### TError
27​
28`TError` = `Error`
29​
30#### TVariables
31​
32`TVariables` = `void`
33​
34#### TOnMutateResult
35​
36`TOnMutateResult` = `unknown`
37​
38### Parameters
39​
40#### options
41​
42[`WithRequired`](../type-aliases/WithRequired.md)\<[`UseMutationOptions`](../interfaces/UseMutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
43​
44The mutation options to use, identical to what you'd pass to `useMutation`, with a
45required `mutationKey`.
46​
47### Returns
48​
49[`WithRequired`](../type-aliases/WithRequired.md)\<[`UseMutationOptions`](../interfaces/UseMutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
50​
51The same options object, unchanged.
52​
53### See
54​
55[useMutation](useMutation.md) to run the mutation these options describe.
56​
57### Example
58​
59Looking the mutation up elsewhere via its `mutationKey`, e.g. for a global "saving…" indicator:
60```tsx
61import { mutationOptions, useMutationState } from '@tanstack/react-query'
62​
63const createPostOptions = mutationOptions({
64 mutationKey: ['posts', 'create'],
65 mutationFn: createPost,
66})
67​
68function SavingIndicator() {
69 const isCreatingPost = useMutationState({
70 filters: { mutationKey: createPostOptions.mutationKey, status: 'pending' },
71 }).length > 0
72​
73 return isCreatingPost ? <span>Saving…</span> : null
75```
76​
77## Call Signature
78​
79```ts
80function mutationOptions<TData, TError, TVariables, TOnMutateResult>(options: Omit<UseMutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">): Omit<UseMutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">;
81```
82​
83Defined in: [packages/react-query/src/mutationOptions.ts:73](https://github.com/TanStack/query/blob/main/packages/react-query/src/mutationOptions.ts#L73)
84​
85You can generally pass everything to `mutationOptions` that you can also pass to `useMutation`. No
86`mutationKey` is required on this overload β€” use this when you don't need to target the mutation via a
87`mutationKey` filter later (e.g. with `useMutationState`); it can still be observed through other filters,
88such as `status`.
89​
90### Type Parameters
91​
92#### TData
93​
94`TData` = `unknown`
95​
96#### TError
97​
98`TError` = `Error`
99​
100#### TVariables
101​
102`TVariables` = `void`
103​
104#### TOnMutateResult
105​
106`TOnMutateResult` = `unknown`
107​
108### Parameters
109​
110#### options
111​
112`Omit`\<[`UseMutationOptions`](../interfaces/UseMutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
113​
114The mutation options to use, identical to what you'd pass to `useMutation`, without a
115`mutationKey`.
116​
117### Returns
118​
119`Omit`\<[`UseMutationOptions`](../interfaces/UseMutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
120​
121The same options object, unchanged.
122​
123### See
124​
125[useMutation](useMutation.md) to run the mutation these options describe.
126​
127### Remarks
128​
129See the other overload's example for looking a mutation up via `useMutationState`.
130​
131### Example
132​
133```tsx
134import { mutationOptions, useMutation } from '@tanstack/react-query'
135​
136export const createPostOptions = mutationOptions({
137 mutationFn: createPost,
138})
139​
140function CreatePost() {
141 const mutation = useMutation(createPostOptions)
142 return <button onClick={() => mutation.mutate({ title: 'Hello' })}>Create</button>
144```