译零 TransZero · TanStack Query v4 中文文档
主站

QueryCache

The QueryCache is the storage mechanism for React Query. It stores all the data, meta information and state of queries it contains.

Normally, you will not interact with the QueryCache directly and instead use the QueryClient for a specific cache.

TypeScript
1import { QueryCache } from "@tanstack/react-query";2 3const queryCache = new QueryCache({4  onError: (error) => {5    console.log(error);6  },7  onSuccess: (data) => {8    console.log(data);9  },10});11 12const query = queryCache.find(["posts"]);

Its available methods are:

Options

  • onError?: (error: unknown, query: Query) => void
    • Optional
    • This function will be called if some query encounters an error.
  • onSuccess?: (data: unknown, query: Query) => void
    • Optional
    • This function will be called if some query is successful.

Global callbacks

The onError and onSuccess 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:

  • defaultOptions can be overridden by each Query - the global callbacks will always be called.
  • defaultOptions callbacks will be called once for each Observer, while the global callbacks will only be called once per Query.

queryCache.find

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.

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)

TypeScript
1const query = queryCache.find(queryKey);

Options

Returns

  • Query
    • The query instance from the cache

queryCache.findAll

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.

Note: This is not typically needed for most applications, but can come in handy when needing more information about a query in rare scenarios

TypeScript
1const queries = queryCache.findAll(queryKey);

Options

Returns

  • Query[]
    • Query instances from the cache

queryCache.subscribe

The 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

TypeScript
1const callback = (event) => {2  console.log(event.type, event.query);3};4 5const unsubscribe = queryCache.subscribe(callback);

Options

  • callback: (event: QueryCacheNotifyEvent) => void
    • 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

Returns

  • unsubscribe: Function => void
    • This function will unsubscribe the callback from the query cache.

queryCache.clear

The clear method can be used to clear the cache entirely and start fresh.

TypeScript
1queryCache.clear();

本页为 TanStack Query 文档(React 版)的非官方中文翻译,原文由上游团队维护,遵循 MIT 许可。 译文如有偏差,以英文原文为准。

本页译文取自社区中文版 react-query-web-i18n(MIT),非本站翻译,已停止更新,内容可能与当前 API 有出入。

译自上游 (未记录版本)