Skip to content

Commit 1b0346f

Browse files
docs: Updates examples for invalidation from mutations (#9261)
* Updates examples for invalidation from mutations * ci: apply automated fixes * Elaborate promise --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 9a3f683 commit 1b0346f

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

docs/framework/react/guides/invalidations-from-mutations.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,25 @@ const queryClient = useQueryClient()
2727
// When this mutation succeeds, invalidate any queries with the `todos` or `reminders` query key
2828
const mutation = useMutation({
2929
mutationFn: addTodo,
30-
onSuccess: () => {
31-
queryClient.invalidateQueries({ queryKey: ['todos'] })
32-
queryClient.invalidateQueries({ queryKey: ['reminders'] })
30+
onSuccess: async () => {
31+
// If you're invalidating a single query
32+
await queryClient.invalidateQueries({ queryKey: ['todos'] })
33+
34+
// If you're invalidating multiple queries
35+
await Promise.all([
36+
queryClient.invalidateQueries({ queryKey: ['todos'] }),
37+
queryClient.invalidateQueries({ queryKey: ['reminders'] }),
38+
])
3339
},
3440
})
3541
```
3642

3743
[//]: # 'Example2'
3844

45+
Returning a Promise on `onSuccess` makes sure the data is updated before the mutation is entirely complete (i.e., isPending is true until onSuccess is fulfilled)
46+
47+
[//]: # 'Example2'
48+
3949
You can wire up your invalidations to happen using any of the callbacks available in the [`useMutation` hook](../mutations.md)
4050

4151
[//]: # 'Materials'

0 commit comments

Comments
 (0)