You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/pages/guides/invalidations-from-mutations.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Invalidating queries is only half the battle. Knowing **when** to invalidate the
8
8
For example, assume we have a mutation to post a new todo:
9
9
10
10
```js
11
-
const[mutate]=useMutation(postTodo)
11
+
constmutation=useMutation(postTodo)
12
12
```
13
13
14
14
When a successful `postTodo` mutation happens, we likely want all `todos` queries to get invalidated and possibly refetched to show the new todo item. To do this, you can use `useMutation`'s `onSuccess` options and the `client`'s `invalidateQueries` function:
`useMutation` comes with some helper options that allow quick and easy side-effects at any stage during the mutation lifecycle. These come in handy for both [invalidating and refetching queries after mutations](../invalidations-from-mutations) and even [optimistic updates](../optimistic-updates)
112
117
113
118
```js
114
-
const [mutate] =useMutation(addTodo, {
115
-
onMutate:(variables)=> {
119
+
useMutation(addTodo, {
120
+
onMutate:variables=> {
116
121
// A mutation is about to happen!
117
122
118
-
// Optionally return a rollbackVariable
119
-
return () => {
120
-
// do some rollback logic
123
+
// Optionally return a context object with a rollback function
The promise returned by `mutate()` can be helpful as well for performing more granular control flow in your app, and if you prefer that that promise only resolves **after**the `onSuccess` or `onSettled` callbacks, you can return a promise in either!:
145
+
When returning a promise in any of the callback functions it will first be awaited before the next callback is called:
137
146
138
147
```js
139
-
const [mutate] =useMutation(addTodo, {
148
+
useMutation(addTodo, {
140
149
onSuccess:async () => {
141
150
console.log("I'm first!")
142
151
},
143
152
onSettled:async () => {
144
153
console.log("I'm second!")
145
154
},
146
155
})
147
-
148
-
mutate(todo)
149
156
```
150
157
151
158
You might find that you want to **add additional side-effects** to some of the `useMutation` lifecycle at the time of calling `mutate`. To do that, you can provide any of the same callback options to the `mutate` function after your mutation variable. Supported option overrides include:
152
159
153
160
-`onSuccess` - Will be fired after the `useMutation`-level `onSuccess` handler
154
161
-`onError` - Will be fired after the `useMutation`-level `onError` handler
155
162
-`onSettled` - Will be fired after the `useMutation`-level `onSettled` handler
156
-
-`throwOnError` - Indicates that the `mutate` function should throw an error if one is encountered
157
163
158
164
```js
159
-
const [mutate] =useMutation(addTodo, {
160
-
onSuccess: (data, mutationVariables) => {
165
+
useMutation(addTodo, {
166
+
onSuccess: (data, variables, context) => {
161
167
// I will fire first
162
168
},
163
-
onError: (error, mutationVariables) => {
169
+
onError: (error, variables, context) => {
164
170
// I will fire first
165
171
},
166
-
onSettled: (data, error, mutationVariables) => {
172
+
onSettled: (data, error, variables, context) => {
167
173
// I will fire first
168
174
},
169
175
})
170
176
171
177
mutate(todo, {
172
-
onSuccess: (data, mutationVariables) => {
178
+
onSuccess: (data, variables, context) => {
173
179
// I will fire second!
174
180
},
175
-
onError: (error, mutationVariables) => {
181
+
onError: (error, variables, context) => {
176
182
// I will fire second!
177
183
},
178
-
onSettled: (data, error, mutationVariables) => {
184
+
onSettled: (data, error, variables, context) => {
179
185
// I will fire second!
180
186
},
181
-
throwOnError:true,
182
187
})
183
188
```
189
+
190
+
## Promises
191
+
192
+
Use `mutateAsync` instead of `mutate` to get a promise which will resolve on success or throw on an error:
These three concepts make up most of the core functionality of React Query. The next sections of the documentation will go over each of these core concepts in great detail.
0 commit comments