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

QueryCache.md

436 lines9.4 KB

1---
2id: QueryCache
3title: QueryCache
4redirect_from:
5 - reference/QueryCache
6 - framework/react/reference/QueryCache
7---
8​
9Defined in: [packages/query-core/src/queryCache.ts:123](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L123)
10​
11The `QueryCache` is the storage mechanism for TanStack Query. It stores all the data, meta
12information, and state of the queries it contains.
13​
14Normally, you will not interact with the `QueryCache` directly and instead use a `QueryClient`
15for a specific cache. You can subscribe to it (inherited from `Subscribable`) to be informed of
16safe/known updates to the cache, such as queries being added, removed, or updated β€” updates made
17outside of the cache's own tracked mechanisms (e.g. mutating a query's state object directly) do
18not notify subscribers.
19​
20## Example
21​
22```ts
23const unsubscribe = queryCache.subscribe((event) => {
24 console.log(event.type, event.query)
25})
26```
27​
28## Extends
29​
30- `Subscribable`\<`QueryCacheListener`\>
31​
32## Constructors
33​
34### Constructor
35​
36```ts
37new QueryCache(config: QueryCacheConfig): QueryCache;
38```
39​
40Defined in: [packages/query-core/src/queryCache.ts:126](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L126)
41​
42#### Parameters
43​
44##### config
45​
46[`QueryCacheConfig`](../interfaces/QueryCacheConfig.md) = `{}`
47​
48#### Returns
49​
50`QueryCache`
51​
52#### Overrides
53​
54```ts
55Subscribable<QueryCacheListener>.constructor
56```
57​
58## Properties
59​
60### config
61​
62```ts
63config: QueryCacheConfig = {};
64```
65​
66Defined in: [packages/query-core/src/queryCache.ts:126](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L126)
67​
68## Methods
69​
70### build()
71​
72```ts
73build<TQueryFnData, TError, TData, TQueryKey>(
74 client: QueryClient,
75 options: WithRequired<QueryOptions<TQueryFnData, TError, TData, TQueryKey, never>, "queryKey">,
76state?: QueryState<TData, TError>): Query<TQueryFnData, TError, TData, TQueryKey>;
77```
78​
79Defined in: [packages/query-core/src/queryCache.ts:147](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L147)
80​
81Returns the existing `Query` instance for the given options' `queryKey`/`queryHash`, or
82builds and adds a new one to the cache if none exists yet. Used by framework adapters and
83plugins (e.g. broadcast/persistence) that need to get-or-create a `Query` directly, bypassing
84the reactive `QueryObserver` machinery.
85​
86#### Type Parameters
87​
88##### TQueryFnData
89​
90`TQueryFnData` = `unknown`
91​
92##### TError
93​
94`TError` = `Error`
95​
96##### TData
97​
98`TData` = `TQueryFnData`
99​
100##### TQueryKey
101​
102`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
103​
104#### Parameters
105​
106##### client
107​
108[`QueryClient`](QueryClient.md)
109​
110##### options
111​
112[`WithRequired`](../type-aliases/WithRequired.md)\<[`QueryOptions`](../interfaces/QueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `never`\>, `"queryKey"`\>
113​
114##### state?
115​
116[`QueryState`](../interfaces/QueryState.md)\<`TData`, `TError`\>
117​
118#### Returns
119​
120[`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
121​
122#### Example
123​
124```ts
125const queryCache = queryClient.getQueryCache()
126​
127const query = queryCache.build(queryClient, {
128 queryKey: ['posts'],
129 queryFn: fetchPosts,
130})
131```
132​
133***
134​
135### clear()
136​
137```ts
138clear(): void;
139```
140​
141Defined in: [packages/query-core/src/queryCache.ts:228](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L228)
142​
143Removes all queries from the cache.
144​
145#### Returns
146​
147`void`
148​
149#### Example
150​
151```ts
152const queryCache = queryClient.getQueryCache()
153​
154queryCache.clear()
155```
156​
157***
158​
159### find()
160​
161```ts
162find<TQueryFnData, TError, TData>(filters: WithRequired<QueryFilters<readonly unknown[]>, "queryKey">):
163 | Query<TQueryFnData, TError, TData, readonly unknown[]>
164 | undefined;
165```
166​
167Defined in: [packages/query-core/src/queryCache.ts:294](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L294)
168​
169A slightly more advanced method that can be used to get an existing query instance from the
170cache. This instance not only contains all the state for the query, but all of the instances,
171and underlying guts of the query as well. If the query does not exist, `undefined` is
172returned.
173​
174This is not typically needed for most applications, but can come in handy when needing more
175information about a query in rare scenarios (e.g. looking at `query.state.dataUpdatedAt` to
176decide whether a query is fresh enough to be used as an initial value).
177​
178#### Type Parameters
179​
180##### TQueryFnData
181​
182`TQueryFnData` = `unknown`
183​
184##### TError
185​
186`TError` = `Error`
187​
188##### TData
189​
190`TData` = `TQueryFnData`
191​
192#### Parameters
193​
194##### filters
195​
196[`WithRequired`](../type-aliases/WithRequired.md)\<[`QueryFilters`](../interfaces/QueryFilters.md)\<readonly `unknown`[]\>, `"queryKey"`\>
197​
198#### Returns
199​
200 \| [`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, readonly `unknown`[]\>
201 \| `undefined`
202​
203#### See
204​
205[QueryCache#findAll](#findall)
206​
207#### Example
208​
209```ts
210const queryCache = queryClient.getQueryCache()
211​
212const query = queryCache.find({ queryKey: ['posts'] })
213```
214​
215***
216​
217### findAll()
218​
219```ts
220findAll(filters: QueryFilters<any>): Query<unknown, Error, unknown, readonly unknown[]>[];
221```
222​
223Defined in: [packages/query-core/src/queryCache.ts:319](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L319)
224​
225An even more advanced method that can be used to get existing query instances from the cache
226that partially match a query key. If no queries match, an empty array is returned.
227​
228This is not typically needed for most applications, but can come in handy when needing more
229information about queries in rare scenarios.
230​
231#### Parameters
232​
233##### filters
234​
235[`QueryFilters`](../interfaces/QueryFilters.md)\<`any`\> = `{}`
236​
237#### Returns
238​
239[`Query`](Query.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[]\>[]
240​
241#### See
242​
243[QueryCache#find](#find)
244​
245#### Example
246​
247```ts
248const queryCache = queryClient.getQueryCache()
249​
250const queries = queryCache.findAll({ queryKey: ['posts'] })
251```
252​
253***
254​
255### get()
256​
257```ts
258get<TQueryFnData, TError, TData, TQueryKey>(queryHash: string):
259 | Query<TQueryFnData, TError, TData, TQueryKey>
260 | undefined;
261```
262​
263Defined in: [packages/query-core/src/queryCache.ts:250](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L250)
264​
265Returns the `Query` instance stored under the given `queryHash`, or `undefined` if none
266exists. Unlike [QueryCache#find](#find), this looks up by the already-computed hash rather
267than by `QueryFilters`. Used by plugins (e.g. broadcast/hydration) that already have a hash
268to look up directly.
269​
270#### Type Parameters
271​
272##### TQueryFnData
273​
274`TQueryFnData` = `unknown`
275​
276##### TError
277​
278`TError` = `Error`
279​
280##### TData
281​
282`TData` = `TQueryFnData`
283​
284##### TQueryKey
285​
286`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
287​
288#### Parameters
289​
290##### queryHash
291​
292`string`
293​
294#### Returns
295​
296 \| [`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
297 \| `undefined`
298​
299#### Example
300​
301```ts
302const queryCache = queryClient.getQueryCache()
303const queryHash = hashKey(['posts'])
304​
305const query = queryCache.get(queryHash)
306```
307​
308***
309​
310### getAll()
311​
312```ts
313getAll(): Query<unknown, Error, unknown, readonly unknown[]>[];
314```
315​
316Defined in: [packages/query-core/src/queryCache.ts:272](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L272)
317​
318Returns all queries within the cache.
319​
320#### Returns
321​
322[`Query`](Query.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[]\>[]
323​
324#### Example
325​
326```ts
327const queryCache = queryClient.getQueryCache()
328​
329const queries = queryCache.getAll()
330```
331​
332***
333​
334### hasListeners()
335​
336```ts
337hasListeners(): boolean;
338```
339​
340Defined in: [packages/query-core/src/subscribable.ts:41](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L41)
341​
342Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
343​
344#### Returns
345​
346`boolean`
347​
348#### Inherited from
349​
350```ts
351Subscribable.hasListeners
352```
353​
354***
355​
356### remove()
357​
358```ts
359remove(query: Query<any, any, any, any>): void;
360```
361​
362Defined in: [packages/query-core/src/queryCache.ts:208](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L208)
363​
364Destroys the given `Query` and removes it from the cache, notifying subscribers with a
365`'removed'` event. A no-op if the query is no longer the one currently stored under its hash
366(e.g. it was already replaced). Used by plugins (e.g. the broadcast client) that mirror
367removals across `QueryCache` instances.
368​
369#### Parameters
370​
371##### query
372​
373[`Query`](Query.md)\<`any`, `any`, `any`, `any`\>
374​
375#### Returns
376​
377`void`
378​
379#### Example
380​
381```ts
382const queryCache = queryClient.getQueryCache()
383const query = queryCache.find({ queryKey: ['posts'] })
384​
385if (query) {
386 queryCache.remove(query)
388```
389​
390***
391​
392### subscribe()
393​
394```ts
395subscribe(listener: QueryCacheListener): () => void;
396```
397​
398Defined in: [packages/query-core/src/subscribable.ts:27](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L27)
399​
400Registers a listener to be called on every update this object notifies about. Returns a function
401that removes the listener again β€” call it to stop listening. The base class never drops a listener
402on its own, though some subclasses clear all of theirs in `destroy()`.
403​
404#### Parameters
405​
406##### listener
407​
408`QueryCacheListener`
409​
410Called on each update, with whatever the subclass passes to its subscribers.
411​
412#### Returns
413​
414```ts
415(): void;
416```
417​
418##### Returns
419​
420`void`
421​
422#### Example
423​
424```ts
425const unsubscribe = subscribable.subscribe(() => {
426 // react to the update
427})
428​
429unsubscribe()
430```
431​
432#### Inherited from
433​
434```ts
435Subscribable.subscribe
436```