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

QueryCache.md

134 lines4.5 KB

1---
2id: QueryCache
3title: QueryCache
4---
5​
6The `QueryCache` is the storage mechanism for TanStack Query. It stores all the data, meta information and state of queries it contains.
7​
8**Normally, you will not interact with the QueryCache directly and instead use the `QueryClient` for a specific cache.**
9​
10```tsx
11import { QueryCache } from '@tanstack/react-query'
12​
13const queryCache = new QueryCache({
14 onError: (error) => {
15 console.log(error)
16 },
17 onSuccess: (data) => {
18 console.log(data)
19 },
20 onSettled: (data, error) => {
21 console.log(data, error)
22 },
23})
24​
25const query = queryCache.find(['posts'])
26```
27​
28Its available methods are:
29​
30- [`find`](#querycachefind)
31- [`findAll`](#querycachefindall)
32- [`subscribe`](#querycachesubscribe)
33- [`clear`](#querycacheclear)
34​
35**Options**
36​
37- `onError?: (error: unknown, query: Query) => void`
38 - Optional
39 - This function will be called if some query encounters an error.
40- `onSuccess?: (data: unknown, query: Query) => void`
41 - Optional
42 - This function will be called if some query is successful.
43- `onSettled?:` (data: unknown | undefined, error: unknown | null, query: Query) => void
44 - Optional
45 - This function will be called if some query is settled (either successful or errored).
46- `createStore?: () => QueryStore`
47 - Optional
48 - This function will be called to create the store that will be used to store the queries. By default, a `Map` is used.
49​
50## Global callbacks
51​
52The `onError`, `onSuccess` and `onSettled` callbacks on the QueryCache can be used to handle these events on a global level. They are different to `defaultOptions` provided to the QueryClient because:
53​
54- `defaultOptions` can be overridden by each Query - the global callbacks will **always** be called.
55- `defaultOptions` callbacks will be called once for each Observer, while the global callbacks will only be called once per Query.
56​
57## `queryCache.find`
58​
59`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.
60​
61> 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)
62​
63```tsx
64const query = queryCache.find(queryKey)
65```
66​
67**Options**
68​
69- `queryKey?: QueryKey`: [Query Keys](../guides/query-keys)
70- `filters?: QueryFilters`: [Query Filters](../guides/filters#query-filters)
71​
72**Returns**
73​
74- `Query`
75 - The query instance from the cache
76​
77## `queryCache.findAll`
78​
79`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.
80​
81> Note: This is not typically needed for most applications, but can come in handy when needing more information about a query in rare scenarios
82​
83```tsx
84const queries = queryCache.findAll(queryKey)
85```
86​
87**Options**
88​
89- `queryKey?: QueryKey`: [Query Keys](../guides/query-keys)
90- `filters?: QueryFilters`: [Query Filters](../guides/filters#query-filters)
91​
92**Returns**
93​
94- `Query[]`
95 - Query instances from the cache
96​
97## `queryCache.subscribe`
98​
99The `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
100​
101```tsx
102const callback = (event) => {
103 console.log(event.type, event.query)
105​
106const unsubscribe = queryCache.subscribe(callback)
107```
108​
109**Options**
110​
111- `callback: (event: QueryCacheNotifyEvent) => void`
112 - 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
113​
114**Returns**
115​
116- `unsubscribe: Function => void`
117 - This function will unsubscribe the callback from the query cache.
118​
119## `queryCache.clear`
120​
121The `clear` method can be used to clear the cache entirely and start fresh.
122​
123```tsx
124queryCache.clear()
125```
126​
127[//]: # 'Materials'
128​
129## Further reading
130​
131To get a better understanding how the QueryCache works internally, have a look at [#18: Inside React Query
132](../community/tkdodos-blog#18-inside-react-query) from the Community Resources.
133​
134[//]: # 'Materials'