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

query-invalidation.md

112 lines2.8 KB

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-experimental'
12​
13class QueryInvalidationExample {
14 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 }))
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'