tanstack-integration-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTanStack Integration Best Practices
TanStack 集成最佳实践
Guidelines for integrating TanStack Query, Router, and Start together effectively. These patterns ensure optimal data flow, caching coordination, and type safety across the stack.
有效集成TanStack Query、Router和Start的指南。这些模式可确保全栈范围内的最优数据流、缓存协调和类型安全。
When to Apply
适用场景
- Setting up a new TanStack Start project
- Integrating TanStack Query with TanStack Router
- Configuring SSR with query hydration
- Coordinating caching between router and query
- Setting up type-safe data fetching patterns
- 搭建新的TanStack Start项目
- 将TanStack Query与TanStack Router集成
- 配置带查询水合的SSR
- 协调路由与查询之间的缓存
- 搭建类型安全的数据获取模式
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Rules | Impact |
|---|---|---|---|
| CRITICAL | Setup | 3 rules | Foundational configuration |
| CRITICAL | SSR Integration | 1 rule | Router + Query SSR setup |
| HIGH | Data Flow | 4 rules | Correct data fetching patterns |
| MEDIUM | Caching | 3 rules | Performance optimization |
| MEDIUM | SSR | 2 rules | Additional SSR patterns |
| 优先级 | 类别 | 规则数量 | 影响 |
|---|---|---|---|
| 关键 | 基础设置 | 3条规则 | 基础配置 |
| 关键 | SSR集成 | 1条规则 | 路由 + 查询SSR设置 |
| 高 | 数据流 | 4条规则 | 正确的数据获取模式 |
| 中 | 缓存 | 3条规则 | 性能优化 |
| 中 | SSR补充 | 2条规则 | 额外的SSR模式 |
Quick Reference
快速参考
Setup (Prefix: setup-
)
setup-基础设置(前缀:setup-
)
setup-- — Pass QueryClient through router context
setup-query-client-context - — Correctly wrap with QueryClientProvider
setup-provider-wrapping - — Coordinate staleTime between router and query
setup-stale-time-coordination
- — 通过路由上下文传递QueryClient
setup-query-client-context - — 正确使用QueryClientProvider进行包裹
setup-provider-wrapping - — 协调路由与查询之间的staleTime
setup-stale-time-coordination
Data Flow (Prefix: flow-
)
flow-数据流(前缀:flow-
)
flow-- — Use loaders with ensureQueryData
flow-loader-query-pattern - — Use useSuspenseQuery in components
flow-suspense-query-component - — Coordinate mutations with query invalidation
flow-mutations-invalidation - — Use server functions for query functions
flow-server-functions-queries
- — 将加载器与ensureQueryData配合使用
flow-loader-query-pattern - — 在组件中使用useSuspenseQuery
flow-suspense-query-component - — 协调变更操作与查询失效
flow-mutations-invalidation - — 将服务器函数用于查询函数
flow-server-functions-queries
Caching (Prefix: cache-
)
cache-缓存(前缀:cache-
)
cache-- — Let TanStack Query manage caching
cache-single-source - — Coordinate preloading between router and query
cache-preload-coordination - — Unified invalidation patterns
cache-invalidation-patterns
- — 让TanStack Query管理缓存
cache-single-source - — 协调路由与查询之间的预加载
cache-preload-coordination - — 统一的失效模式
cache-invalidation-patterns
SSR Integration (Prefix: ssr-
)
ssr-SSR集成(前缀:ssr-
)
ssr-- — Use setupRouterSsrQueryIntegration for automatic SSR
ssr-dehydrate-hydrate
- — 使用setupRouterSsrQueryIntegration实现自动SSR
ssr-dehydrate-hydrate
Additional SSR (Prefix: ssr-
)
ssr-SSR补充(前缀:ssr-
)
ssr-- — Create QueryClient per request
ssr-per-request-client - — Handle streaming with queries
ssr-streaming-queries
- — 为每个请求创建QueryClient
ssr-per-request-client - — 处理带查询的流式传输
ssr-streaming-queries
How to Use
使用方法
Each rule file in the directory contains:
rules/- Explanation — Why this pattern matters
- Bad Example — Anti-pattern to avoid
- Good Example — Recommended implementation
- Context — When to apply or skip this rule
rules/- 说明 — 该模式的重要性
- 反面示例 — 需要避免的反模式
- 正面示例 — 推荐的实现方式
- 适用场景 — 何时应用或跳过该规则
Full Reference
完整参考
See individual rule files in directory for detailed guidance and code examples.
rules/请查看目录下的单个规则文件,获取详细指导和代码示例。
rules/