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