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

QueryCache.md

125 lines4.0 KB

1---
2id: QueryCache
3title: QueryCache
4redirect_from:
5 - framework/react/reference/QueryCache
6---
7​
8The `QueryCache` is the storage mechanism for TanStack Query. It stores all the data, meta information and state of queries it contains.
9​
10**Normally, you will not interact with the QueryCache directly and instead use the `QueryClient` for a specific cache.**
11​
12```tsx
13import { QueryCache } from '@tanstack/react-query'
14​
15const queryCache = new QueryCache({
16 onError: (error) => {
17 console.log(error)
18 },
19 onSuccess: (data) => {
20 console.log(data)
21 },
22 onSettled: (data, error) => {
23 console.log(data, error)
24 },
25})
26​
27const query = queryCache.find({ queryKey: ['posts'] })
28```
29​
30Its available methods are:
31​
32- [`queryCache.find`](#querycache-find)
33- [`queryCache.findAll`](#querycache-findall)
34- [`queryCache.subscribe`](#querycache-subscribe)
35- [`queryCache.clear`](#querycache-clear)
36- [Further reading](#further-reading)
37​
38**Options**
39​
40- `onError?: (error: unknown, query: Query) => void`
41 - Optional
42 - This function will be called if some query encounters an error.
43- `onSuccess?: (data: unknown, query: Query) => void`
44 - Optional
45 - This function will be called if some query is successful.
46- `onSettled?: (data: unknown | undefined, error: unknown | null, query: Query) => void`
47 - Optional
48 - This function will be called if some query is settled (either successful or errored).
49​
50## `queryCache.find`
51​
52`find` is a slightly more advanced synchronous method that can be used to get an existing query instance from the cache. This instance not only contains **all** the state for the query, but all of the instances, and underlying guts of the query as well. If the query does not exist, `undefined` will be returned.
53​
54> Note: This is not typically needed for most applications, but can come in handy when needing more information about a query in rare scenarios (eg. Looking at the query.state.dataUpdatedAt timestamp to decide whether a query is fresh enough to be used as an initial value)
55​
56```tsx
57const query = queryCache.find({ queryKey })
58```
59​
60**Options**
61​
62- `filters: QueryFilters`: [Query Filters](../framework/react/guides/filters#query-filters)
63 - `queryKey: QueryKey`: [Query Keys](../framework/react/guides/query-keys.md)
64​
65**Returns**
66​
67- `Query`
68 - The query instance from the cache
69​
70## `queryCache.findAll`
71​
72`findAll` is even more advanced synchronous method that can be used to get existing query instances from the cache that partially match query key. If queries do not exist, empty array will be returned.
73​
74> Note: This is not typically needed for most applications, but can come in handy when needing more information about a query in rare scenarios
75​
76```tsx
77const queries = queryCache.findAll({ queryKey })
78```
79​
80**Options**
81​
82- `filters?: QueryFilters`: [Query Filters](../framework/react/guides/filters.md#query-filters)
83​
84**Returns**
85​
86- `Query[]`
87 - Query instances from the cache
88​
89## `queryCache.subscribe`
90​
91The `subscribe` method can be used to subscribe to the query cache as a whole and be informed of safe/known updates to the cache like query states changing or queries being updated, added or removed
92​
93```tsx
94const callback = (event) => {
95 console.log(event.type, event.query)
97​
98const unsubscribe = queryCache.subscribe(callback)
99```
100​
101**Options**
102​
103- `callback: (event: QueryCacheNotifyEvent) => void`
104 - This function will be called with the query cache any time it is updated via its tracked update mechanisms (eg, `query.setState`, `queryClient.removeQueries`, etc). Out of scope mutations to the cache are not encouraged and will not fire subscription callbacks
105​
106**Returns**
107​
108- `unsubscribe: Function => void`
109 - This function will unsubscribe the callback from the query cache.
110​
111## `queryCache.clear`
112​
113The `clear` method can be used to clear the cache entirely and start fresh.
114​
115```tsx
116queryCache.clear()
117```
118​
119[//]: # 'Materials'
120​
121## Further reading
122​
123To get a better understanding how the QueryCache works internally, have a look at [the Inside React Query article by TkDodo](https://tkdodo.eu/blog/inside-react-query).
124​
125[//]: # 'Materials'