TanStack Router is a fully type-safe router for React that brings the best of file-based routing with the power of TypeScript — every route, param, and search query is type-checked.
Why TanStack Router Matters
A developer using React Router had runtime crashes from typos in route paths and missing URL params that TypeScript couldn't catch. TanStack Router catches all of these at compile time.
Key Features:
- 100% Type-Safe — Routes, params, search params, all typed
- File-Based Routing — Automatic route generation from file system
- Search Params — First-class URL search param management
- Data Loading — Built-in loaders with caching
- Devtools — Visual route debugging
Quick Start
npm install @tanstack/react-router
import { createRouter, createRoute, createRootRoute } from "@tanstack/react-router"
const rootRoute = createRootRoute({ component: () => <Outlet /> })
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: "/",
component: () => <h1>Home</h1>
})
const postRoute = createRoute({
getParentRoute: () => rootRoute,
path: "/posts/$postId",
loader: ({ params }) => fetchPost(params.postId), // postId is typed!
component: ({ useLoaderData }) => {
const post = useLoaderData()
return <h1>{post.title}</h1>
}
})
Type-Safe Search Params
const searchRoute = createRoute({
path: "/search",
validateSearch: (search) => ({ q: String(search.q || ""), page: Number(search.page || 1) }),
component: () => {
const { q, page } = searchRoute.useSearch() // Fully typed!
return <Results query={q} page={page} />
}
})
Why Choose TanStack Router
- Type safety everywhere — no runtime routing errors
- URL state management — search params as first-class citizens
- Built-in data loading — with caching and preloading
Check out TanStack Router docs to get started.
Building React apps? Check out my Apify actors or email spinov001@gmail.com for custom solutions.
Top comments (0)