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

QueryCache.md

104 lines3.3 KB

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)
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```