Open History to see the last commit
1---
2id: invalidation-from-mutations
3title: Invalidation from Mutations
4---
5β
6Invalidating queries is only half the battle. Knowing **when** to invalidate them is the other half. Usually when a mutation in your app succeeds, it's VERY likely that there are related queries in your application that need to be invalidated and possibly refetched to account for the new changes from your mutation.
7β
8For example, assume we have a mutation to post a new todo:
9β
10```tsx
11const mutation = useMutation(postTodo)
12```
13β
14When 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:
15β
16```tsx
17import { useMutation, useQueryClient } from '@tanstack/react-query'
18β
19const queryClient = useQueryClient()
20β
21// When this mutation succeeds, invalidate any queries with the `todos` or `reminders` query key
22const mutation = useMutation(addTodo, {
23 onSuccess: () => {
24 queryClient.invalidateQueries(['todos'])
25 queryClient.invalidateQueries(['reminders'])
26 },
27})
28```
29β
30You can wire up your invalidations to happen using any of the callbacks available in the [`useMutation` hook](../guides/mutations)