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

QueryCache.md

492 lines10.3 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***
69​
70### listeners
71​
72```ts
73protected listeners: Set<QueryCacheListener>;
74```
75​
76Defined in: [packages/query-core/src/subscribable.ts:7](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L7)
77​
78#### Inherited from
79​
80```ts
81Subscribable.listeners
82```
83​
84## Methods
85​
86### build()
87​
88```ts
89build<TQueryFnData, TError, TData, TQueryKey>(
90 client: QueryClient,
91 options: WithRequired<QueryOptions<TQueryFnData, TError, TData, TQueryKey, never>, "queryKey">,
92state?: QueryState<TData, TError>): Query<TQueryFnData, TError, TData, TQueryKey>;
93```
94​
95Defined in: [packages/query-core/src/queryCache.ts:147](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L147)
96​
97Returns the existing `Query` instance for the given options' `queryKey`/`queryHash`, or
98builds and adds a new one to the cache if none exists yet. Used by framework adapters and
99plugins (e.g. broadcast/persistence) that need to get-or-create a `Query` directly, bypassing
100the reactive `QueryObserver` machinery.
101​
102#### Type Parameters
103​
104##### TQueryFnData
105​
106`TQueryFnData` = `unknown`
107​
108##### TError
109​
110`TError` = `Error`
111​
112##### TData
113​
114`TData` = `TQueryFnData`
115​
116##### TQueryKey
117​
118`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
119​
120#### Parameters
121​
122##### client
123​
124[`QueryClient`](QueryClient.md)
125​
126##### options
127​
128[`WithRequired`](../type-aliases/WithRequired.md)\<[`QueryOptions`](../interfaces/QueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `never`\>, `"queryKey"`\>
129​
130##### state?
131​
132[`QueryState`](../interfaces/QueryState.md)\<`TData`, `TError`\>
133​
134#### Returns
135​
136[`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
137​
138#### Example
139​
140```ts
141const queryCache = queryClient.getQueryCache()
142​
143const query = queryCache.build(queryClient, {
144 queryKey: ['posts'],
145 queryFn: fetchPosts,
146})
147```
148​
149***
150​
151### clear()
152​
153```ts
154clear(): void;
155```
156​
157Defined in: [packages/query-core/src/queryCache.ts:228](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L228)
158​
159Removes all queries from the cache.
160​
161#### Returns
162​
163`void`
164​
165#### Example
166​
167```ts
168const queryCache = queryClient.getQueryCache()
169​
170queryCache.clear()
171```
172​
173***
174​
175### find()
176​
177```ts
178find<TQueryFnData, TError, TData>(filters: WithRequired<QueryFilters<readonly unknown[]>, "queryKey">):
179 | Query<TQueryFnData, TError, TData, readonly unknown[]>
180 | undefined;
181```
182​
183Defined in: [packages/query-core/src/queryCache.ts:295](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L295)
184​
185A slightly more advanced method that can be used to get an existing query instance from the
186cache. This instance not only contains all the state for the query, but all of the instances,
187and underlying guts of the query as well. If the query does not exist, `undefined` is
188returned.
189​
190This is not typically needed for most applications, but can come in handy when needing more
191information about a query in rare scenarios (e.g. looking at `query.state.dataUpdatedAt` to
192decide whether a query is fresh enough to be used as an initial value).
193​
194#### Type Parameters
195​
196##### TQueryFnData
197​
198`TQueryFnData` = `unknown`
199​
200##### TError
201​
202`TError` = `Error`
203​
204##### TData
205​
206`TData` = `TQueryFnData`
207​
208#### Parameters
209​
210##### filters
211​
212[`WithRequired`](../type-aliases/WithRequired.md)\<[`QueryFilters`](../interfaces/QueryFilters.md)\<readonly `unknown`[]\>, `"queryKey"`\>
213​
214#### Returns
215​
216 \| [`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, readonly `unknown`[]\>
217 \| `undefined`
218​
219#### See
220​
221[QueryCache#findAll](#findall)
222​
223#### Example
224​
225```ts
226const queryCache = queryClient.getQueryCache()
227​
228const query = queryCache.find({ queryKey: ['posts'] })
229```
230​
231***
232​
233### findAll()
234​
235```ts
236findAll(filters: QueryFilters<any>): Query<unknown, Error, unknown, readonly unknown[]>[];
237```
238​
239Defined in: [packages/query-core/src/queryCache.ts:320](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L320)
240​
241An even more advanced method that can be used to get existing query instances from the cache
242that partially match a query key. If no queries match, an empty array is returned.
243​
244This is not typically needed for most applications, but can come in handy when needing more
245information about queries in rare scenarios.
246​
247#### Parameters
248​
249##### filters
250​
251[`QueryFilters`](../interfaces/QueryFilters.md)\<`any`\> = `{}`
252​
253#### Returns
254​
255[`Query`](Query.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[]\>[]
256​
257#### See
258​
259[QueryCache#find](#find)
260​
261#### Example
262​
263```ts
264const queryCache = queryClient.getQueryCache()
265​
266const queries = queryCache.findAll({ queryKey: ['posts'] })
267```
268​
269***
270​
271### get()
272​
273```ts
274get<TQueryFnData, TError, TData, TQueryKey>(queryHash: string):
275 | Query<TQueryFnData, TError, TData, TQueryKey>
276 | undefined;
277```
278​
279Defined in: [packages/query-core/src/queryCache.ts:250](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L250)
280​
281Returns the `Query` instance stored under the given `queryHash`, or `undefined` if none
282exists. Unlike [QueryCache#find](#find), this looks up by the already-computed hash rather
283than by `QueryFilters`. Used by plugins (e.g. broadcast/hydration) that already have a hash
284to look up directly.
285​
286#### Type Parameters
287​
288##### TQueryFnData
289​
290`TQueryFnData` = `unknown`
291​
292##### TError
293​
294`TError` = `Error`
295​
296##### TData
297​
298`TData` = `TQueryFnData`
299​
300##### TQueryKey
301​
302`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
303​
304#### Parameters
305​
306##### queryHash
307​
308`string`
309​
310#### Returns
311​
312 \| [`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
313 \| `undefined`
314​
315#### Example
316​
317```ts
318const queryCache = queryClient.getQueryCache()
319const queryHash = hashKey(['posts'])
320​
321const query = queryCache.get(queryHash)
322```
323​
324***
325​
326### getAll()
327​
328```ts
329getAll(): Query<unknown, Error, unknown, readonly unknown[]>[];
330```
331​
332Defined in: [packages/query-core/src/queryCache.ts:273](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L273)
333​
334Returns all queries within the cache.
335​
336#### Returns
337​
338[`Query`](Query.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[]\>[]
339​
340#### Example
341​
342```ts
343const queryCache = queryClient.getQueryCache()
344​
345const queries = queryCache.getAll()
346```
347​
348***
349​
350### hasListeners()
351​
352```ts
353hasListeners(): boolean;
354```
355​
356Defined in: [packages/query-core/src/subscribable.ts:41](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L41)
357​
358Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
359​
360#### Returns
361​
362`boolean`
363​
364#### Inherited from
365​
366```ts
367Subscribable.hasListeners
368```
369​
370***
371​
372### onSubscribe()
373​
374```ts
375protected onSubscribe(): void;
376```
377​
378Defined in: [packages/query-core/src/subscribable.ts:45](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L45)
379​
380#### Returns
381​
382`void`
383​
384#### Inherited from
385​
386```ts
387Subscribable.onSubscribe
388```
389​
390***
391​
392### onUnsubscribe()
393​
394```ts
395protected onUnsubscribe(): void;
396```
397​
398Defined in: [packages/query-core/src/subscribable.ts:49](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L49)
399​
400#### Returns
401​
402`void`
403​
404#### Inherited from
405​
406```ts
407Subscribable.onUnsubscribe
408```
409​
410***
411​
412### remove()
413​
414```ts
415remove(query: Query<any, any, any, any>): void;
416```
417​
418Defined in: [packages/query-core/src/queryCache.ts:208](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L208)
419​
420Destroys the given `Query` and removes it from the cache, notifying subscribers with a
421`'removed'` event. A no-op if the query is no longer the one currently stored under its hash
422(e.g. it was already replaced). Used by plugins (e.g. the broadcast client) that mirror
423removals across `QueryCache` instances.
424​
425#### Parameters
426​
427##### query
428​
429[`Query`](Query.md)\<`any`, `any`, `any`, `any`\>
430​
431#### Returns
432​
433`void`
434​
435#### Example
436​
437```ts
438const queryCache = queryClient.getQueryCache()
439const query = queryCache.find({ queryKey: ['posts'] })
440​
441if (query) {
442 queryCache.remove(query)
444```
445​
446***
447​
448### subscribe()
449​
450```ts
451subscribe(listener: QueryCacheListener): () => void;
452```
453​
454Defined in: [packages/query-core/src/subscribable.ts:27](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L27)
455​
456Registers a listener to be called on every update this object notifies about. Returns a function
457that removes the listener again β€” call it to stop listening. The base class never drops a listener
458on its own, though some subclasses clear all of theirs in `destroy()`.
459​
460#### Parameters
461​
462##### listener
463​
464`QueryCacheListener`
465​
466Called on each update, with whatever the subclass passes to its subscribers.
467​
468#### Returns
469​
470```ts
471(): void;
472```
473​
474##### Returns
475​
476`void`
477​
478#### Example
479​
480```ts
481const unsubscribe = subscribable.subscribe(() => {
482 // react to the update
483})
484​
485unsubscribe()
486```
487​
488#### Inherited from
489​
490```ts
491Subscribable.subscribe
492```