Open History to see the last commit
1---
2id: query-invalidation
3title: Query Invalidation
4ref: docs/framework/react/guides/query-invalidation.md
5replace: { 'useQuery': 'injectQuery', 'hooks': 'functions' }
6---
7β
8[//]: # 'Example2'
9β
10```ts
11import { injectQuery, QueryClient } from '@tanstack/angular-query'
12β
13class QueryInvalidationExample {
14 readonly queryClient = inject(QueryClient)
15β
16 invalidateQueries() {
17 this.queryClient.invalidateQueries({ queryKey: ['todos'] })
18 }
19β
20 // Both queries below will be invalidated
21 todoListQuery = injectQuery(() => ({
22 queryKey: ['todos'],
23 queryFn: fetchTodoList,
24 }))
25 todoListQuery = injectQuery(() => ({
26 queryKey: ['todos', { page: 1 }],
27 queryFn: fetchTodoList,
28 }))
29}
30```
31β
32[//]: # 'Example2'
33β
34You can even invalidate queries with specific variables by passing a more specific query key to the `invalidateQueries` method:
35β
36[//]: # 'Example3'
37β
38```ts
39queryClient.invalidateQueries({
40 queryKey: ['todos', { type: 'done' }],
41})
42β
43// The query below will be invalidated
44todoListQuery = injectQuery(() => ({
45 queryKey: ['todos', { type: 'done' }],
46 queryFn: fetchTodoList,
47}))
48β
49// However, the following query below will NOT be invalidated
50todoListQuery = injectQuery(() => ({
51 queryKey: ['todos'],
52 queryFn: fetchTodoList,
53}))
54```
55β
56[//]: # 'Example3'
57β
58The `invalidateQueries` API is very flexible, so even if you want to **only** invalidate `todos` queries that don't have any more variables or subkeys, you can pass an `exact: true` option to the `invalidateQueries` method:
59β
60[//]: # 'Example4'
61β
62```ts
63queryClient.invalidateQueries({
64 queryKey: ['todos'],
65 exact: true,
66})
67β
68// The query below will be invalidated
69todoListQuery = injectQuery(() => ({
70 queryKey: ['todos'],
71 queryFn: fetchTodoList,
72}))
73β
74// However, the following query below will NOT be invalidated
75const todoListQuery = injectQuery(() => ({
76 queryKey: ['todos', { type: 'done' }],
77 queryFn: fetchTodoList,
78}))
79```
80β
81[//]: # 'Example4'
82β
83If you find yourself wanting **even more** granularity, you can pass a predicate function to the `invalidateQueries` method. This function will receive each `Query` instance from the query cache and allow you to return `true` or `false` for whether you want to invalidate that query:
84β
85[//]: # 'Example5'
86β
87```ts
88queryClient.invalidateQueries({
89 predicate: (query) =>
90 query.queryKey[0] === 'todos' && query.queryKey[1]?.version >= 10,
91})
92β
93// The query below will be invalidated
94todoListQuery = injectQuery(() => ({
95 queryKey: ['todos', { version: 20 }],
96 queryFn: fetchTodoList,
97}))
98β
99// The query below will be invalidated
100todoListQuery = injectQuery(() => ({
101 queryKey: ['todos', { version: 10 }],
102 queryFn: fetchTodoList,
103}))
104β
105// However, the following query below will NOT be invalidated
106todoListQuery = injectQuery(() => ({
107 queryKey: ['todos', { version: 5 }],
108 queryFn: fetchTodoList,
109}))
110```
111β
112[//]: # 'Example5'