Open History to see the last commit
1---
2id: QueryCache
3title: QueryCache
4---
5β
6The `QueryCache` is the storage mechanism for React Query. It stores all of 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```js
11import { QueryCache } from 'react-query'
12β
13const queryCache = new QueryCache({
14 onError: error => {
15 console.log(error)
16 },
17})
18β
19const query = queryCache.find('posts')
20```
21β
22Its available methods are:
23β
24- [`find`](#querycachefind)
25- [`findAll`](#querycachefindall)
26- [`subscribe`](#querycachesubscribe)
27- [`clear`](#querycacheclear)
28β
29**Options**
30β
31- `onError?: (error: unknown, query: Query) => void`
32 - Optional
33 - This function will be called if some query encounters an error.
34β
35## `queryCache.find`
36β
37`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.
38β
39> 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)
40β
41```js
42const query = queryCache.find(queryKey)
43```
44β
45**Options**
46β
47- `queryKey?: QueryKey`: [Query Keys](../guides/query-keys)
48- `filters?: QueryFilters`: [Query Filters](../guides/query-filters)
49β
50**Returns**
51β
52- `Query`
53 - The query instance from the cache
54β
55## `queryCache.findAll`
56β
57`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.
58β
59> Note: This is not typically needed for most applications, but can come in handy when needing more information about a query in rare scenarios
60β
61```js
62const queries = queryCache.findAll(queryKey)
63```
64β
65**Options**
66β
67- `queryKey?: QueryKey`: [Query Keys](../guides/query-keys)
68- `filters?: QueryFilters`: [Query Filters](../guides/query-filters)
69β
70**Returns**
71β
72- `Query[]`
73 - Query instances from the cache
74β
75## `queryCache.subscribe`
76β
77The `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
78β
79```js
80const callback = query => {
81 console.log(query)
82}
83β
84const unsubscribe = queryCache.subscribe(callback)
85```
86β
87**Options**
88β
89- `callback: (query?: Query) => void`
90 - 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
91 - Additionally, for updates to the cache triggered by a specific query, the `query` will be passed as first argument to the callback
92β
93**Returns**
94β
95- `unsubscribe: Function => void`
96 - This function will unsubscribe the callback from the query cache.
97β
98## `queryCache.clear`
99β
100The `clear` method can be used to clear the cache entirely and start fresh.
101β
102```js
103queryCache.clear()
104```