Loading...
Loading...
Compare original and translation side by side
CRITICAL: TanStack Router types are FULLY INFERRED. Never cast, never annotate inferred values. This is the #1 AI agent mistake.
CRITICAL: TanStack Router is CLIENT-FIRST. Loaders run on the client by default, NOT server-only like Remix/Next.js. Do not confuse TanStack Router APIs with Next.js or React Router.
重要提示:TanStack Router 的类型是完全自动推断的。永远不要对推断出的值进行类型断言,也不要手动添加类型注解。这是AI Agent最常犯的错误。
重要提示:TanStack Router 是客户端优先的。加载器默认在客户端运行,不像 Remix/Next.js 那样仅在服务端运行。不要将 TanStack Router 的API和 Next.js 或 React Router 的API混淆。
| Task | Sub-Skill |
|---|---|
| Validate, read, write, transform search params | router-core/search-params/SKILL.md |
| Dynamic segments, splats, optional params | router-core/path-params/SKILL.md |
| Link, useNavigate, preloading, blocking | router-core/navigation/SKILL.md |
| Route loaders, SWR caching, context, deferred data | router-core/data-loading/SKILL.md |
| Auth guards, RBAC, beforeLoad redirects | router-core/auth-and-guards/SKILL.md |
| Automatic and manual code splitting | router-core/code-splitting/SKILL.md |
| 404 handling, error boundaries, notFound() | router-core/not-found-and-errors/SKILL.md |
| Inference, Register, from narrowing, TS perf | router-core/type-safety/SKILL.md |
| Streaming/non-streaming SSR, hydration, head mgmt | router-core/ssr/SKILL.md |
| 任务 | 子技能 |
|---|---|
| 验证、读取、写入、转换搜索参数 | router-core/search-params/SKILL.md |
| 动态路由段、通配符、可选参数 | router-core/path-params/SKILL.md |
| Link、useNavigate、预加载、路由阻断 | router-core/navigation/SKILL.md |
| 路由加载器、SWR 缓存、上下文、延迟数据 | router-core/data-loading/SKILL.md |
| 鉴权守卫、RBAC、beforeLoad 重定向 | router-core/auth-and-guards/SKILL.md |
| 自动与手动代码分割 | router-core/code-splitting/SKILL.md |
| 404 处理、错误边界、notFound() | router-core/not-found-and-errors/SKILL.md |
| 类型推断、Register、类型收窄、TS 性能优化 | router-core/type-safety/SKILL.md |
| 流式/非流式 SSR、 hydration、head 管理 | router-core/ssr/SKILL.md |
Need to add/read/write URL query parameters?
→ router-core/search-params
Need dynamic URL segments like /posts/$postId?
→ router-core/path-params
Need to create links or navigate programmatically?
→ router-core/navigation
Need to fetch data for a route?
Is it client-side only or client+server?
→ router-core/data-loading
Using TanStack Query as external cache?
→ compositions/router-query (separate skill)
Need to protect routes behind auth?
→ router-core/auth-and-guards
Need to reduce bundle size per route?
→ router-core/code-splitting
Need custom 404 or error handling?
→ router-core/not-found-and-errors
Having TypeScript issues or performance problems?
→ router-core/type-safety
Need server-side rendering?
→ router-core/ssrNeed to add/read/write URL query parameters?
→ router-core/search-params
Need dynamic URL segments like /posts/$postId?
→ router-core/path-params
Need to create links or navigate programmatically?
→ router-core/navigation
Need to fetch data for a route?
Is it client-side only or client+server?
→ router-core/data-loading
Using TanStack Query as external cache?
→ compositions/router-query (separate skill)
Need to protect routes behind auth?
→ router-core/auth-and-guards
Need to reduce bundle size per route?
→ router-core/code-splitting
Need custom 404 or error handling?
→ router-core/not-found-and-errors
Having TypeScript issues or performance problems?
→ router-core/type-safety
Need server-side rendering?
→ router-core/ssr// src/routes/__root.tsx
import { createRootRoute, Outlet } from '@tanstack/react-router'
export const Route = createRootRoute({
component: () => <Outlet />,
})// src/routes/index.tsx
import { createFileRoute } from '@tanstack/react-router'
export const Route = createFileRoute('/')({
component: () => <h1>Home</h1>,
})// src/router.tsx
import { createRouter } from '@tanstack/react-router'
import { routeTree } from './routeTree.gen'
const router = createRouter({ routeTree })
// REQUIRED for type safety — without this, Link/useNavigate have no autocomplete
declare module '@tanstack/react-router' {
interface Register {
router: typeof router
}
}
export default router// src/main.tsx
import { RouterProvider } from '@tanstack/react-router'
import router from './router'
function App() {
return <RouterProvider router={router} />
}// src/routes/__root.tsx
import { createRootRoute, Outlet } from '@tanstack/react-router'
export const Route = createRootRoute({
component: () => <Outlet />,
})// src/routes/index.tsx
import { createFileRoute } from '@tanstack/react-router'
export const Route = createFileRoute('/')({
component: () => <h1>首页</h1>,
})// src/router.tsx
import { createRouter } from '@tanstack/react-router'
import { routeTree } from './routeTree.gen'
const router = createRouter({ routeTree })
// 类型安全必须配置 — 缺少该配置的话,Link/useNavigate 不会有自动补全
declare module '@tanstack/react-router' {
interface Register {
router: typeof router
}
}
export default router// src/main.tsx
import { RouterProvider } from '@tanstack/react-router'
import router from './router'
function App() {
return <RouterProvider router={router} />
}createFileRoutesrc/routes/// File: src/routes/posts/$postId.tsx
export const Route = createFileRoute('/posts/$postId')({
// ✅ matches file path
component: PostPage,
})
export const Route = createFileRoute('/post/$postId')({
// ❌ silent mismatch
component: PostPage,
})createFileRoutesrc/routes/// 文件路径: src/routes/posts/$postId.tsx
export const Route = createFileRoute('/posts/$postId')({
// ✅ 和文件路径匹配
component: PostPage,
})
export const Route = createFileRoute('/post/$postId')({
// ❌ 无提示的路径不匹配错误
component: PostPage,
})@tanstack/router-core@tanstack/react-router$**@tanstack/router-core@tanstack/react-router$**