tanstack router
via PatrickJS/awesome-cursorrules
Type-safe file-based routing for React with TanStack Router v1, loaders, and search params validation.
What is tanstack router?
TanStack Router v1 enables 100% type-safe client-side routing in React apps using file-based conventions, TypeScript generics, and Zod validation. Use this rule when building scalable React applications that require strict type safety for route params, search params, loader data, and navigation.
- Define type-safe routes with file-based routing conventions and createFileRoute/createRootRoute
- Validate search params with Zod schemas and access them via Route.useSearch()
- Load route data in loader functions with TanStack Query integration and context injection
- Implement auth guards and redirects using beforeLoad hooks
- Use typed Link components for navigation with compile-time param validation
- Enable automatic prefetching and code splitting for performance optimization
Applies to
File patterns this rule matches.
Rule definition (reference)
Source of truth, from the repository.
You are an expert in TanStack Router v1, React, TypeScript, and type-safe client-side routing.
Core Principles
- TanStack Router is 100% type-safe — leverage TypeScript generics for params, search params, and loader data
- Prefer file-based routing with
@tanstack/router-vite-pluginfor scalability - Always define routes with
createFileRouteorcreateRootRoute - Route data loading belongs in
loaderfunctions, not in componentuseEffect - Search params are first-class — always define their schema with Zod for type safety
File-Based Route Conventions
src/routes/
__root.tsx ← Root layout
index.tsx ← / route
posts/
index.tsx ← /posts
$postId.tsx ← /posts/:postId (dynamic)
_layout.tsx ← Layout route (no path segment)
_auth/ ← Pathless auth layout group
dashboard.tsx
Route Definition
export const Route = createFileRoute('/posts/$postId')({
loader: async ({ params }) => fetchPost(params.postId),
component: PostComponent,
errorComponent: ({ error }) => <ErrorBanner message={error.message} />,
pendingComponent: () => <PostSkeleton />,
})
function PostComponent() {
const post = Route.useLoaderData() // type-safe
const { postId } = Route.useParams() // type-safe
return <div>{post.title}</div>
}
Type-Safe Search Params
- Always define search params with Zod and
validateSearch - Access with
Route.useSearch()— never readwindow.location.searchdirectly
const searchSchema = z.object({
page: z.number().int().min(1).default(1),
q: z.string().optional(),
})
export const Route = createFileRoute('/search')({
validateSearch: searchSchema,
component: SearchPage,
})
Navigation
- Use
<Link>for internal navigation — never<a href> - Always pass typed
paramsandsearch— the compiler will catch mistakes
<Link to="/posts/$postId" params={{ postId: '123' }}>View Post</Link>
Loaders + TanStack Query Integration
export const Route = createFileRoute('/posts')({
loader: ({ context: { queryClient } }) =>
queryClient.ensureQueryData(postsQueryOptions()),
component: PostsPage,
})
Router Context for Dependency Injection
// __root.tsx
interface RouterContext { queryClient: QueryClient; auth: AuthState }
export const Route = createRootRouteWithContext<RouterContext>()({ component: RootLayout })
// main.tsx
const router = createRouter({ routeTree, context: { queryClient, auth } })
Auth Guards
export const Route = createFileRoute('/_auth/dashboard')({
beforeLoad: ({ context }) => {
if (!context.auth.isAuthenticated) throw redirect({ to: '/login' })
},
component: Dashboard,
})
Performance
- Set
defaultPreload: 'intent'on router for automatic prefetching on hover/focus - Use
React.lazyfor route component code splitting - Install
@tanstack/router-devtoolsand render<TanStackRouterDevtools />in development
Related rules
Type-safe file-based routing for React with TanStack Router v1, loaders, and search validation.
Full-stack React framework with type-safe server functions, file-based routing, and streaming via TanStack Router + Vinxi.
Full-stack React framework with server functions, streaming, and end-to-end type safety.
Expert guidance for building desktop apps with Tauri, Svelte, and TypeScript.
Best practices and conventions for building Temporal.io workflows and activities in Python.
TensorFlow and deep learning best practices for building, training, and deploying neural networks
