tanstack-router
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTanStack Router
TanStack Router
Type-safe, file-based routing for React with route-level data loading, search params validation, code splitting, and TanStack Query integration.
Package: | Plugin:
@tanstack/react-router@tanstack/router-plugin这是为React打造的类型安全、基于文件的路由方案,支持路由级数据加载、搜索参数验证、代码分割以及TanStack Query集成。
包: | 插件:
@tanstack/react-router@tanstack/router-pluginQuick Reference
速查参考
| Pattern | Usage |
|---|---|
| Define file-based route |
| Root route with typed context |
| Code-split route component |
| Search params validation |
| Access loader data in component |
| Type-safe route params |
| Type-safe search params |
| Programmatic navigation |
| Block navigation (dirty forms) |
| Throw 404 from loader |
| Type-safe hooks in split files |
| Clean default values from URLs |
| Preserve params across navs |
| 模式 | 用途 |
|---|---|
| 定义基于文件的路由 |
| 带类型化上下文的根路由 |
| 代码分割的路由组件 |
| 搜索参数验证 |
| 在组件中访问加载器数据 |
| 类型安全的路由参数 |
| 类型安全的搜索参数 |
| 编程式导航 |
| 阻止导航(针对未保存的表单) |
| 从加载器中抛出404错误 |
| 在拆分文件中使用类型安全的钩子 |
| 从URL中清除默认值 |
| 在导航过程中保留指定参数 |
Data Loading
数据加载
| Method | Returns | Throws | Use Case |
|---|---|---|---|
| Data | Yes | Route loaders (recommended) |
| void | No | Background prefetching |
| Data | Yes | Immediate data need |
| Promise | No | Stream non-critical data |
| 方法 | 返回值 | 是否抛出错误 | 使用场景 |
|---|---|---|---|
| 数据 | 是 | 路由加载器(推荐使用) |
| void | 否 | 后台预加载 |
| 数据 | 是 | 即时获取数据需求 |
| Promise | 否 | 流式加载非关键数据 |
Preloading
预加载
| Strategy | Behavior | Use Case |
|---|---|---|
| Preload on hover/focus | Default for most links |
| Preload when Link mounts | Critical next pages |
| Preload when Link in view | Below-fold content |
| No preloading | Heavy, rarely-visited pages |
| 策略 | 行为 | 使用场景 |
|---|---|---|
| 在悬停/聚焦时预加载 | 大多数链接的默认策略 |
| 当Link组件挂载时预加载 | 关键的下一级页面 |
| 当Link进入视口时预加载 | 页面下方的内容 |
| 不进行预加载 | 资源密集、极少访问的页面 |
File Organization
文件组织
| File | Purpose |
|---|---|
| Root route with |
| Index route for |
| Dynamic param route |
| Pathless layout (auth guard) |
| Code-split component |
| 文件名称 | 用途 |
|---|---|
| 包含 |
| 对应 |
| 带动态参数的路由 |
| 无路径布局(认证守卫) |
| 经过代码分割的组件 |
Common Mistakes
常见错误
| Mistake | Fix |
|---|---|
| Missing router type registration | Add |
| Always pass |
| Use |
| Use |
Fetching in | Use route loaders (prevents waterfalls) |
| Sequential fetches in loader | Use |
| Missing leading slash | Always |
| TanStackRouterVite after react() | Plugin MUST come before |
| Use strict mode or manually parse after navigation |
| Pathless route notFoundComponent | Define |
| Aborted loader undefined error | Guard |
No | Declare deps so loader only re-runs when they change |
| 错误内容 | 修复方案 |
|---|---|
| 缺少路由类型注册 | 添加包含 |
使用 | 始终传入 |
用 | 对 |
在加载器中使用 | 使用 |
在 | 使用路由加载器(避免请求瀑布流) |
| 在加载器中按顺序发起请求 | 使用 |
| 路径缺少前导斜杠 | 始终使用 |
Vite配置中 | 插件必须在Vite配置中 |
| 使用严格模式,或在导航后手动解析参数 |
无路径路由的 | 改为在子路由上定义 |
| 加载器中止时出现undefined错误 | 在 |
未声明 | 声明依赖项,使加载器仅在依赖变化时重新运行 |
Delegation
相关技能委托
- TanStack Query patterns — data fetching, caching, mutations: use skill
tanstack-query - TanStack Start — server functions, SSR, server-side auth: use skill
tanstack-start - TanStack Table — table rendering with router search params: use skill
tanstack-table - Router + Query integration — coordinated patterns: use skill
tanstack-integration
- TanStack Query 模式 — 数据请求、缓存、变更:使用技能
tanstack-query - TanStack Start — 服务器函数、SSR、服务端认证:使用技能
tanstack-start - TanStack Table — 结合路由搜索参数渲染表格:使用技能
tanstack-table - 路由 + Query 集成 — 协同使用模式:使用技能
tanstack-integration
References
参考资料
- Setup — installation, Vite config, file structure, app setup, router default options
- Type Safety — register router, param,
from, type utilities, getRouteApistrict: false - Data Loading — route loaders, Query integration, parallel loading, streaming, deferred data, abort signal, loaderDeps
- Search Params — validation, strip/retain middleware, fine-grained subscriptions, debounce, custom serializers
- Navigation — Link, active styling, relative navigation, hash, route masks, blocker, scroll restoration
- Auth and Context — beforeLoad, context inheritance, pathless layouts, dependency injection, error handling
- Code Splitting — lazy routes, auto splitting, preloading strategies, programmatic preloading
- Virtual File Routes — rootRoute, route, index, layout, physical builders, mixing file-based and code-based routing
- Known Issues — 20 documented issues with fixes, anti-patterns
- Loader+Query Patterns — ensureQueryData in loaders, parallel loading, critical vs non-critical data, search-param-dependent loaders
- 搭建指南 — 安装、Vite配置、文件结构、应用搭建、路由默认选项
- 类型安全 — 注册路由、参数、
from、类型工具、getRouteApistrict: false - 数据加载 — 路由加载器、Query集成、并行加载、流式加载、延迟数据、中止信号、loaderDeps
- 搜索参数 — 验证、清除/保留中间件、细粒度订阅、防抖、自定义序列化器
- 导航 — Link组件、激活状态样式、相对导航、哈希、路由掩码、导航拦截、滚动恢复
- 认证与上下文 — beforeLoad、上下文继承、无路径布局、依赖注入、错误处理
- 代码分割 — 懒加载路由、自动分割、预加载策略、编程式预加载
- 虚拟文件路由 — rootRoute、路由、索引、布局、物理构建器、混合基于文件和代码的路由
- 已知问题 — 20个已记录的问题及修复方案、反模式
- 加载器+Query模式 — 在加载器中使用ensureQueryData、并行加载、关键与非关键数据、依赖搜索参数的加载器