tanstack-query
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTanStack Query
TanStack Query
Use this skill when work touches TanStack Query v5, especially , server-state caching, query/mutation lifecycle issues, SSR hydration, or migrations from React Query/TanStack Query v4.
@tanstack/react-query当你处理TanStack Query v5相关工作时,尤其是涉及、服务端状态缓存、查询/突变生命周期问题、SSR hydration或从React Query/TanStack Query v4迁移的场景,可使用此技能。
@tanstack/react-queryWorkflow
工作流程
- Inspect the local Query shape before changing code:
- Package versions for ,
@tanstack/react-query, devtools, persist/broadcast packages, ESLint plugin, React, framework router, and test utilities.@tanstack/query-core - Provider setup: ,
QueryClient, SSR request scoping, default options, devtools, and persistence providers.QueryClientProvider - Query conventions: query key factories, ,
queryOptions, custom hooks, router loaders, SSR prefetching, and invalidation helpers.mutationOptions - Runtime intent: client-only fetching, server prefetch and hydration, route preloading, offline/persistence, or multi-tab sync.
- Package versions for
- Refresh docs for current behavior, package drift, SSR, persistence, or migrations. Start from source-map.md.
- For installation, provider setup, defaults, query keys, , and TypeScript options, use setup-core.md.
useQuery - For dependent, parallel, paginated, infinite, disabled, cancellable, selected, and performance-sensitive queries, use query-patterns.md.
- For , invalidation, direct cache writes, optimistic updates, mutation scopes, and offline mutations, use mutations-cache.md.
useMutation - For prefetching, router integration, SSR, hydration, Suspense, Server Components, and streaming caveats, use ssr-prefetching.md.
- For persistence, broadcast sync, devtools, ESLint, testing, and migration checks, use production-tooling.md.
- 在修改代码前检查本地Query结构:
- 、
@tanstack/react-query、开发工具、持久化/广播包、ESLint插件、React、框架路由和测试工具的版本。@tanstack/query-core - 提供者设置:、
QueryClient、SSR请求作用域、默认选项、开发工具和持久化提供者。QueryClientProvider - 查询约定:查询键工厂、、
queryOptions、自定义钩子、路由加载器、SSR预获取和失效处理助手。mutationOptions - 运行时意图:仅客户端获取、服务端预获取与hydration、路由预加载、离线/持久化或多标签页同步。
- 查阅文档了解当前行为、包版本差异、SSR、持久化或迁移相关内容。从source-map.md开始。
- 关于安装、提供者设置、默认值、查询键、和TypeScript选项,请参考setup-core.md。
useQuery - 关于依赖查询、并行查询、分页查询、无限查询、禁用查询、可取消查询、选择查询和性能敏感型查询,请参考query-patterns.md。
- 关于、失效处理、直接缓存写入、乐观更新、突变作用域和离线突变,请参考mutations-cache.md。
useMutation - 关于预获取、路由集成、SSR、hydration、Suspense、服务端组件和流式处理注意事项,请参考ssr-prefetching.md。
- 关于持久化、广播同步、开发工具、ESLint、测试和迁移检查,请参考production-tooling.md。
Implementation Judgment
实现判断
- Treat TanStack Query as server-state management, not a replacement for local client UI state.
- Query keys are cache identity. Include every changing variable used by the query function, and keep keys serializable.
- Prefer object syntax everywhere in v5. Do not reintroduce positional v3/v4 signatures.
- Prefer typed and
queryOptionshelpers for reusable queries and mutations.mutationOptions - Set deliberately. Default stale data refetches on mount, window focus, and reconnect can be correct, but noisy when misunderstood.
staleTime - Use targeted invalidation as the default after mutations. Use immutable when the mutation response already contains the exact updated object.
setQueryData - For optimistic updates, prefer UI-level when only one surface needs the pending state; use cache-level
variablesrollback when multiple surfaces must react.onMutate - In SSR, create request-scoped query clients on the server and a stable browser client. Avoid module-level server caches that leak data across users.
- In tests, isolate each test with its own , turn retries off, and clear or recreate clients rather than sharing cache state.
QueryClient
- 将TanStack Query视为服务端状态管理工具,而非本地客户端UI状态的替代方案。
- 查询键是缓存的标识。需包含查询函数使用的所有可变变量,并保持键可序列化。
- 在v5中优先使用对象语法。不要重新引入v3/v4的位置参数签名。
- 对于可复用的查询和突变,优先使用带类型的和
queryOptions助手。mutationOptions - 谨慎设置。默认的 stale 数据在挂载、窗口聚焦和重新连接时重新获取可能是正确的,但如果不理解其逻辑会产生不必要的请求。
staleTime - 突变后默认使用针对性的失效处理。当突变响应已包含精确的更新对象时,使用不可变的。
setQueryData - 对于乐观更新,当仅单个界面需要待处理状态时,优先使用UI层面的;当多个界面必须做出响应时,使用缓存层面的
variables回滚。onMutate - 在SSR中,在服务端创建请求作用域的查询客户端,并使用稳定的浏览器客户端。避免使用模块级别的服务端缓存,以免在用户之间泄露数据。
- 在测试中,为每个测试单独创建,关闭重试机制,清除或重新创建客户端而非共享缓存状态。
QueryClient
Verification
验证
Prefer the repo's existing checks. For meaningful TanStack Query changes, include the relevant subset:
- Typecheck for query keys, query function returns, , mutation variables, and SSR hydration types.
select - Focused tests for invalidation, optimistic rollback, cache updates, dependent queries, pagination, infinite queries, and error states.
- Browser smoke for loading, stale-data, background-fetching, pagination, mutation pending/error, hydration, and focus/reconnect behavior.
- SSR or route-loader smoke when changing prefetching, hydration, stale times, or provider placement.
- ESLint Query plugin checks when the repo uses .
@tanstack/eslint-plugin-query
优先使用仓库现有的检查机制。对于重要的TanStack Query变更,需包含以下相关子集:
- 对查询键、查询函数返回值、、突变变量和SSR hydration类型进行类型检查。
select - 针对失效处理、乐观回滚、缓存更新、依赖查询、分页、无限查询和错误状态进行专项测试。
- 针对加载状态、stale数据、后台获取、分页、突变待处理/错误状态、hydration以及聚焦/重新连接行为进行浏览器冒烟测试。
- 当修改预获取、hydration、stale时间或提供者位置时,进行SSR或路由加载器冒烟测试。
- 当仓库使用时,进行ESLint Query插件检查。
@tanstack/eslint-plugin-query