Skip to content

Commit f2bfc69

Browse files
zrwskTkDodo
andauthored
docs(mutations): describe difference between using callbacks in useMu… (#3274)
* docs(mutations): describe difference between using callbacks in useMutation and .mutate * Text corrections Co-authored-by: Dominik Dorfmeister <[email protected]> * add subheader * update consecutive mutations case to better describe ordering of mutation calls and fulfillment Co-authored-by: Dominik Dorfmeister <[email protected]>
1 parent 9b5d18c commit f2bfc69

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

docs/src/pages/guides/mutations.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,28 @@ mutate(todo, {
179179
})
180180
```
181181

182+
### Consecutive mutations
183+
There is a slight difference in handling `onSuccess`, `onError` and `onSettled` callbacks when it comes to consecutive mutations. When passed to the `mutate` function, they will be fired up only _once_ and only if the component is still mounted. This is due to the fact that mutation observer is removed and resubscribed every time when the `mutate` function is called. On the contrary, `useMutation` handlers execute for each `mutate` call.
184+
185+
> Be aware that most likely, `mutationFn` passed to `useMutation` is ansynchronous. In that case, the order in which mutations are fulfilled may differ from the order of `mutate` function calls.
186+
187+
```js
188+
useMutation(addTodo, {
189+
onSuccess: (data, error, variables, context) => {
190+
// Will be called 3 times
191+
},
192+
})
193+
194+
['Todo 1', 'Todo 2', 'Todo 3'].forEach((todo) => {
195+
mutate(todo, {
196+
onSuccess: (data, error, variables, context) => {
197+
// Will execute only once, for the last mutation (Todo 3),
198+
// regardles which mutation resolves first
199+
},
200+
})
201+
})
202+
203+
```
182204
## Promises
183205

184206
Use `mutateAsync` instead of `mutate` to get a promise which will resolve on success or throw on an error. This can for example be used to compose side effects.

0 commit comments

Comments
 (0)