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

invalidations-from-mutations.md

43 lines1.1 KB

1---
2id: invalidations-from-mutations
3title: Invalidations from Mutations
4ref: docs/react/guides/invalidations-from-mutations.md
5replace: { 'useMutation': 'injectMutation', 'hook': 'function' }
6---
7​
8[//]: # 'Example'
9​
10```ts
11class TodoItemComponent {
12 mutation = injectMutation(() => ({
13 mutationFn: postTodo,
14 }))
16```
17​
18[//]: # 'Example'
19[//]: # 'Example2'
20​
21```ts
22import { injectMutation } from '@tanstack/angular-query-experimental'
23​
24export class TodosComponent {
25 queryClient = injectQueryClient()
26​
27 // When this mutation succeeds, invalidate any queries with the `todos` or `reminders` query key
28 mutation = injectMutation((client) => ({
29 mutationFn: addTodo,
30 onSuccess: () => {
31 client.invalidateQueries({ queryKey: ['todos'] })
32 client.invalidateQueries({ queryKey: ['reminders'] })
33​
34 // OR use the queryClient that is injected into the component
35 // this.queryClient.invalidateQueries({ queryKey: ['todos'] })
36 },
37 }))
39```
40​
41[//]: # 'Example2'
42​
43You can wire up your invalidations to happen using any of the callbacks available in the [`injectMutation` function](../guides/mutations)