tanstack-router-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTanStack Router Best Practices
TanStack Router 最佳实践
Comprehensive guidelines for implementing TanStack Router patterns in React applications. These rules optimize type safety, data loading, navigation, and code organization.
在 React 应用中实现 TanStack Router 模式的综合指南。这些规则可优化类型安全性、数据加载、导航和代码组织。
When to Apply
适用场景
- Setting up application routing
- Creating new routes and layouts
- Implementing search parameter handling
- Configuring data loaders
- Setting up code splitting
- Integrating with TanStack Query
- Refactoring navigation patterns
- 搭建应用路由
- 创建新路由与布局
- 实现搜索参数处理
- 配置数据加载器
- 搭建代码分割
- 与 TanStack Query 集成
- 重构导航模式
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Rules | Impact |
|---|---|---|---|
| CRITICAL | Type Safety | 4 rules | Prevents runtime errors and enables refactoring |
| CRITICAL | Route Organization | 5 rules | Ensures maintainable route structure |
| HIGH | Router Config | 1 rule | Global router defaults |
| HIGH | Data Loading | 6 rules | Optimizes data fetching and caching |
| HIGH | Search Params | 5 rules | Enables type-safe URL state |
| HIGH | Error Handling | 1 rule | Handles 404 and errors gracefully |
| MEDIUM | Navigation | 5 rules | Improves UX and accessibility |
| MEDIUM | Code Splitting | 3 rules | Reduces bundle size |
| MEDIUM | Preloading | 3 rules | Improves perceived performance |
| LOW | Route Context | 3 rules | Enables dependency injection |
| 优先级 | 类别 | 规则数量 | 影响 |
|---|---|---|---|
| 关键 | 类型安全 | 4 条规则 | 防止运行时错误并支持重构 |
| 关键 | 路由组织 | 5 条规则 | 确保路由结构可维护 |
| 高 | 路由器配置 | 1 条规则 | 全局路由器默认设置 |
| 高 | 数据加载 | 6 条规则 | 优化数据获取与缓存 |
| 高 | 搜索参数 | 5 条规则 | 实现类型安全的 URL 状态 |
| 高 | 错误处理 | 1 条规则 | 优雅处理 404 与错误 |
| 中 | 导航 | 5 条规则 | 提升用户体验与可访问性 |
| 中 | 代码分割 | 3 条规则 | 减小包体积 |
| 中 | 预加载 | 3 条规则 | 提升感知性能 |
| 低 | 路由上下文 | 3 条规则 | 支持依赖注入 |
Quick Reference
快速参考
Type Safety (Prefix: ts-
)
ts-类型安全(前缀:ts-
)
ts-- — Register router type for global inference
ts-register-router - — Use
ts-use-from-paramparameter for type narrowingfrom - — Type route context with createRootRouteWithContext
ts-route-context-typing - — Use queryOptions in loaders for type inference
ts-query-options-loader
- — 注册路由器类型以实现全局推断
ts-register-router - — 使用
ts-use-from-param参数进行类型收窄from - — 用 createRootRouteWithContext 为路由上下文添加类型
ts-route-context-typing - — 在加载器中使用 queryOptions 进行类型推断
ts-query-options-loader
Router Config (Prefix: router-
)
router-路由器配置(前缀:router-
)
router-- — Configure router defaults (scrollRestoration, defaultErrorComponent, etc.)
router-default-options
- — 配置路由器默认选项(scrollRestoration、defaultErrorComponent 等)
router-default-options
Route Organization (Prefix: org-
)
org-路由组织(前缀:org-
)
org-- — Prefer file-based routing for conventions
org-file-based-routing - — Follow hierarchical route tree patterns
org-route-tree-structure - — Use pathless routes for shared layouts
org-pathless-layouts - — Understand index vs layout routes
org-index-routes - — Understand virtual file routes
org-virtual-routes
- — 优先使用基于文件的路由约定
org-file-based-routing - — 遵循分层路由树模式
org-route-tree-structure - — 使用无路径路由实现共享布局
org-pathless-layouts - — 理解索引路由与布局路由的区别
org-index-routes - — 理解虚拟文件路由
org-virtual-routes
Data Loading (Prefix: load-
)
load-数据加载(前缀:load-
)
load-- — Use route loaders for data fetching
load-use-loaders - — Define loaderDeps for cache control
load-loader-deps - — Use ensureQueryData with TanStack Query
load-ensure-query-data - — Split critical and non-critical data
load-deferred-data - — Handle loader errors appropriately
load-error-handling - — Leverage parallel route loading
load-parallel
- — 使用路由加载器进行数据获取
load-use-loaders - — 定义 loaderDeps 以控制缓存
load-loader-deps - — 结合 TanStack Query 使用 ensureQueryData
load-ensure-query-data - — 拆分关键与非关键数据
load-deferred-data - — 正确处理加载器错误
load-error-handling - — 利用并行路由加载
load-parallel
Search Params (Prefix: search-
)
search-搜索参数(前缀:search-
)
search-- — Always validate search params
search-validation - — Leverage parent search param types
search-type-inheritance - — Use search param middleware
search-middleware - — Provide sensible defaults
search-defaults - — Configure custom search param serializers
search-custom-serializer
- — 始终验证搜索参数
search-validation - — 利用父级搜索参数类型
search-type-inheritance - — 使用搜索参数中间件
search-middleware - — 提供合理的默认值
search-defaults - — 配置自定义搜索参数序列化器
search-custom-serializer
Error Handling (Prefix: err-
)
err-错误处理(前缀:err-
)
err-- — Handle not-found routes properly
err-not-found
- — 正确处理未找到的路由
err-not-found
Navigation (Prefix: nav-
)
nav-导航(前缀:nav-
)
nav-- — Prefer Link component for navigation
nav-link-component - — Configure active link states
nav-active-states - — Use useNavigate for programmatic navigation
nav-use-navigate - — Understand relative path navigation
nav-relative-paths - — Use route masks for modal URLs
nav-route-masks
- — 优先使用 Link 组件进行导航
nav-link-component - — 配置活动链接状态
nav-active-states - — 使用 useNavigate 进行编程式导航
nav-use-navigate - — 理解相对路径导航
nav-relative-paths - — 使用路由掩码实现模态框 URL
nav-route-masks
Code Splitting (Prefix: split-
)
split-代码分割(前缀:split-
)
split-- — Use .lazy.tsx for code splitting
split-lazy-routes - — Keep critical config in main route file
split-critical-path - — Enable autoCodeSplitting when possible
split-auto-splitting
- — 使用 .lazy.tsx 进行代码分割
split-lazy-routes - — 将关键配置保留在主路由文件中
split-critical-path - — 尽可能启用 autoCodeSplitting
split-auto-splitting
Preloading (Prefix: preload-
)
preload-预加载(前缀:preload-
)
preload-- — Enable intent-based preloading
preload-intent - — Configure preload stale time
preload-stale-time - — Use manual preloading strategically
preload-manual
- — 启用基于意图的预加载
preload-intent - — 配置预加载过期时间
preload-stale-time - — 策略性地使用手动预加载
preload-manual
Route Context (Prefix: ctx-
)
ctx-路由上下文(前缀:ctx-
)
ctx-- — Define context at root route
ctx-root-context - — Extend context in beforeLoad
ctx-before-load - — Use context for dependency injection
ctx-dependency-injection
- — 在根路由定义上下文
ctx-root-context - — 在 beforeLoad 中扩展上下文
ctx-before-load - — 使用上下文进行依赖注入
ctx-dependency-injection
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/