tanstack-query
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTanStack Query Best Practices
TanStack Query 最佳实践
Comprehensive performance optimization guide for TanStack Query v5 applications. Contains 40 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
针对TanStack Query v5应用的全面性能优化指南。包含8个类别共40条规则,按影响优先级排序,可指导自动化重构和代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Writing new queries, mutations, or data fetching logic
- Implementing caching strategies (staleTime, gcTime)
- Reviewing code for performance issues or request waterfalls
- Refactoring existing TanStack Query code
- Implementing infinite queries, Suspense, or optimistic updates
在以下场景中参考这些指南:
- 编写新的查询、Mutations或数据获取逻辑
- 实现缓存策略(staleTime、gcTime)
- 审查代码以排查性能问题或请求瀑布
- 重构现有TanStack Query代码
- 实现无限查询、Suspense或乐观更新
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Query Key Structure | CRITICAL | |
| 2 | Caching Configuration | CRITICAL | |
| 3 | Mutation Patterns | HIGH | |
| 4 | Prefetching & Waterfalls | HIGH | |
| 5 | Infinite Queries | MEDIUM | |
| 6 | Suspense Integration | MEDIUM | |
| 7 | Error & Retry Handling | MEDIUM | |
| 8 | Render Optimization | LOW-MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 查询键结构 | 关键 | |
| 2 | 缓存配置 | 关键 | |
| 3 | Mutation模式 | 高 | |
| 4 | 预取与请求瀑布 | 高 | |
| 5 | 无限查询 | 中 | |
| 6 | Suspense集成 | 中 | |
| 7 | 错误与重试处理 | 中 | |
| 8 | 渲染优化 | 低-中 | |
Quick Reference
快速参考
1. Query Key Structure (CRITICAL)
1. 查询键结构(关键)
- - Use centralized query key factories
tquery-key-factories - - Structure keys from generic to specific
tquery-hierarchical-keys - - Always use array query keys
tquery-always-arrays - - Use serializable objects in keys
tquery-serializable-objects - - Use queryOptions for type-safe sharing
tquery-options-pattern - - Colocate query keys with features
tquery-colocate-keys
- - 使用集中式查询键工厂
tquery-key-factories - - 按从通用到具体的结构组织键
tquery-hierarchical-keys - - 始终使用数组形式的查询键
tquery-always-arrays - - 在键中使用可序列化对象
tquery-serializable-objects - - 使用queryOptions实现类型安全的共享
tquery-options-pattern - - 将查询键与功能代码放在一起
tquery-colocate-keys
2. Caching Configuration (CRITICAL)
2. 缓存配置(关键)
- - Understand staleTime vs gcTime
cache-staletime-gctime - - Configure global defaults appropriately
cache-global-defaults - - Use placeholderData vs initialData correctly
cache-placeholder-vs-initial - - Invalidate with precision
cache-invalidation-precision - - Control automatic refetch triggers
cache-refetch-triggers - - Use enabled for conditional queries
cache-enabled-option
- - 理解staleTime与gcTime的区别
cache-staletime-gctime - - 合理配置全局默认值
cache-global-defaults - - 正确使用placeholderData和initialData
cache-placeholder-vs-initial - - 精准地使缓存失效
cache-invalidation-precision - - 控制自动重新获取的触发条件
cache-refetch-triggers - - 使用enabled实现条件查询
cache-enabled-option
3. Mutation Patterns (HIGH)
3. Mutation模式(高)
- - Implement optimistic updates with rollback
mutation-optimistic-updates - - Invalidate in onSettled, not onSuccess
mutation-invalidate-onsettled - - Cancel queries before optimistic updates
mutation-cancel-queries - - Use setQueryData for immediate cache updates
mutation-setquerydata - - Avoid parallel mutations on same data
mutation-avoid-parallel
- - 实现带回滚的乐观更新
mutation-optimistic-updates - - 在onSettled中使缓存失效,而非onSuccess
mutation-invalidate-onsettled - - 在乐观更新前取消查询
mutation-cancel-queries - - 使用setQueryData实现即时缓存更新
mutation-setquerydata - - 避免对同一数据执行并行Mutations
mutation-avoid-parallel
4. Prefetching & Waterfalls (HIGH)
4. 预取与请求瀑布(高)
- - Avoid request waterfalls
prefetch-avoid-waterfalls - - Prefetch on hover for perceived speed
prefetch-on-hover - - Prefetch dependent data in queryFn
prefetch-in-queryfn - - Prefetch in Server Components
prefetch-server-components - - Flatten API to reduce waterfalls
prefetch-flatten-api
- - 避免请求瀑布
prefetch-avoid-waterfalls - - 在悬停时预取以提升感知速度
prefetch-on-hover - - 在queryFn中预取依赖数据
prefetch-in-queryfn - - 在Server Components中预取
prefetch-server-components - - 扁平化API以减少请求瀑布
prefetch-flatten-api
5. Infinite Queries (MEDIUM)
5. 无限查询(中)
- - Limit infinite query pages with maxPages
infinite-max-pages - - Flatten pages for rendering
infinite-flatten-pages - - Understand infinite query refetch behavior
infinite-refetch-behavior - - Handle infinite query loading states correctly
infinite-loading-states
- - 使用maxPages限制无限查询的页数
infinite-max-pages - - 扁平化页面数据以用于渲染
infinite-flatten-pages - - 理解无限查询的重新获取行为
infinite-refetch-behavior - - 正确处理无限查询的加载状态
infinite-loading-states
6. Suspense Integration (MEDIUM)
6. Suspense集成(中)
- - Use Suspense hooks for simpler loading states
suspense-use-suspense-hooks - - Always pair Suspense with Error Boundaries
suspense-error-boundaries - - Combine Suspense queries with useSuspenseQueries
suspense-parallel-queries - - Place Suspense boundaries strategically
suspense-boundaries-placement
- - 使用Suspense钩子简化加载状态处理
suspense-use-suspense-hooks - - 始终将Suspense与Error Boundaries配合使用
suspense-error-boundaries - - 使用useSuspenseQueries组合Suspense查询
suspense-parallel-queries - - 合理放置Suspense边界
suspense-boundaries-placement
7. Error & Retry Handling (MEDIUM)
7. 错误与重试处理(中)
- - Configure retry with exponential backoff
error-retry-config - - Use conditional retry based on error type
error-conditional-retry - - Use global error handler for common errors
error-global-handler - - Display errors appropriately
error-display-patterns - - Use throwOnError with Error Boundaries
error-throw-on-error
- - 配置带指数退避的重试机制
error-retry-config - - 根据错误类型实现条件重试
error-conditional-retry - - 使用全局错误处理程序处理常见错误
error-global-handler - - 合理展示错误信息
error-display-patterns - - 将throwOnError与Error Boundaries配合使用
error-throw-on-error
8. Render Optimization (LOW-MEDIUM)
8. 渲染优化(低-中)
- - Memoize select functions
render-select-memoize - - Use select to derive data and reduce re-renders
render-select-derived - - Use notifyOnChangeProps to limit re-renders
render-notify-props - - Understand structural sharing
render-structural-sharing - - Avoid destructuring all properties
render-tracked-props
- - 对select函数进行记忆化处理
render-select-memoize - - 使用select派生数据以减少重渲染
render-select-derived - - 使用notifyOnChangeProps限制重渲染
render-notify-props - - 理解结构共享机制
render-structural-sharing - - 避免解构所有属性
render-tracked-props
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Reference files:
references/{prefix}-{slug}.md
Each reference file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references
阅读各个参考文件以获取详细说明和代码示例:
- 章节定义 - 类别结构和影响级别
- 参考文件:
references/{prefix}-{slug}.md
每个参考文件包含:
- 简要说明其重要性
- 错误代码示例及解释
- 正确代码示例及解释
- 额外背景信息和参考资料
Related Skills
相关技能
- For generating type-safe query hooks, see skill
orval - For mocking API responses in tests, see skill
test-msw - For React 19 data fetching patterns, see skill
react-19
- 如需生成类型安全的查询钩子,请查看技能
orval - 如需在测试中模拟API响应,请查看技能
test-msw - 如需了解React 19数据获取模式,请查看技能
react-19
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md如需查看包含所有展开规则的完整指南:
AGENTS.md