Command Palette

Search for a command to run...

UnylyUnyly
Back to skills

tanstack-router-guide

FreeNo executable scriptsNot checked

Type-safe, file-based router for React with first-class search params, data loading, and code splitting. Use when user asks to "create routes with TanStack…

vcode-shby vcode-sh★ 19 GitHub stars

About this skill

TanStack Router Guide (React)

TanStack Router is a fully type-safe, file-based router for React. It provides first-class search param APIs, built-in data loading with SWR caching, automatic code splitting, and 100% inferred TypeScript types. Designed for client-first SPAs with optional SSR support.

Install

npm install @tanstack/react-router
npm install -D @tanstack/router-plugin
# Optional: devtools
npm install @tanstack/react-router-devtools

Quick Start with Vite (Recommended)

1. Configure Vite:

// vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { tanstackRouter } from '@tanstack/router-plugin/vite'

export default defineConfig({
  plugins: [
    tanstackRouter({ target: 'react', autoCodeSplitting: true }),
    react(), // Must come AFTER tanstackRouter
  ],
})

2. Create root route:

// src/routes/__root.tsx
import { createRootRoute, Link, Outlet } from '@tanstack/react-router'
import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'

export const Route = createRootRoute({
  component: () => (
    <>
      <nav>
        <Link to="/" activeProps={{ className: 'font-bold' }}>Home</Link>
        <Link to="/about" activeProps={{ className: 'font-bold' }}>About</Link>
      </nav>
      <Outlet />
      <TanStackRouterDevtools />
    </>
  ),
})

3. Create routes:

// src/routes/index.tsx
import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/')({
  component: () => <div>Welcome Home!</div>,
})

// src/routes/about.tsx
export const Route = createFileRoute('/about')({
  component: () => <div>About Page</div>,
})

4. Mount the router:

// src/main.tsx
import { StrictMode } from 'react'
import ReactDOM from 'react-dom/client'
import { RouterProvider, createRouter } from '@tanstack/react-router'
import { routeTree } from './routeTree.gen'

const router = createRouter({ routeTree })

// Register router type globally for type safety
declare module '@tanstack/react-router' {
  interface Register {
    router: typeof router
  }
}

ReactDOM.createRoot(document.getElementById('root')!).render(
  <StrictMode>
    <RouterProvider router={router} />
  </StrictMode>,
)

File-Based Routing (Naming Conventions)

Routes live in src/routes/ and file names determine URL paths:

File Name URL Path Purpose
__root.tsx N/A Root layout (always rendered)
index.tsx / Index route
about.tsx /about Static route
posts.tsx /posts Layout route (renders Outlet)
posts.index.tsx /posts Index for /posts
posts.$postId.tsx /posts/:postId Dynamic segment
_auth.tsx N/A Pathless layout (wraps children, no URL)
_auth.dashboard.tsx /dashboard Child of pathless layout
posts_.$postId.edit.tsx /posts/:postId/edit Non-nested (escapes parent layout)
files.$.tsx /files/* Splat/catch-all route
posts.{-$category}.tsx /posts/:category? Optional path parameter
-utils.tsx N/A Excluded from routing
(group)/login.tsx /login Route group (organizational only)

The plugin auto-generates routeTree.gen.ts - commit this file but never edit it manually.

Navigation

import { Link, useNavigate } from '@tanstack/react-router'

// Declarative - Link component
<Link to="/posts/$postId" params={{ postId: '123' }}>View Post</Link>
<Link to="/posts" search={{ page: 2, sort: 'asc' }}>Page 2</Link>
<Link to=".." from="/posts/$postId">Back to Posts</Link>

// Active styling
<Link to="/about" activeProps={{ className: 'active' }} inactiveProps={{ className: 'dim' }}>
  About
</Link>

// Programmatic - useNavigate
const navigate = useNavigate()
navigate({ to: '/posts/$postId', params: { postId: '123' } })
navigate({ to: '/posts', search: (prev) => ({ ...prev, page: 2 }) })
navigate({ to: '..', from: '/posts/$postId' }) // Relative

Search Params (Validated & Type-Safe)

import { createFileRoute } from '@tanstack/react-router'
import { z } from 'zod'

export const Route = createFileRoute('/posts')({
  validateSearch: z.object({
    page: z.number().catch(1),
    sort: z.enum(['asc', 'desc']).optional(),
    filter: z.string().optional(),
  }),
  component: PostsPage,
})

function PostsPage() {
  const { page, sort, filter } = Route.useSearch() // Fully typed
  const navigate = Route.useNavigate()

  return (
    <button onClick={() => navigate({ search: (prev) => ({ ...prev, page: prev.page + 1 }) })}>
      Next Page
    </button>
  )
}

Search Middlewares - retain or strip params across navigations:

import { retainSearchParams, stripSearchParams } from '@tanstack/react-router'

export const Route = createFileRoute('/posts')({
  validateSearch: z.object({ page: z.number().catch(1), q: z.string().optional() }),
  search: {
    middlewares: [
      retainSearchP
Full SKILL.md on GitHub

Install tanstack-router-guide in Claude Code & Claude Desktop

Sign up to install this skill

Create a free account to reveal the install command and save the skill to your library.

  • Reveal the one-line install command
  • Save skills to your synced library
  • Get notified when skills update
Sign up freeI already have an account

Allowed tools

Tools this skill is permitted to call.

No restriction — this skill can use any tool.

Bundled files

references/advanced-code-splitting.mdreferences/advanced-head-scroll.mdreferences/advanced-optimization.mdreferences/advanced-ssr.mdreferences/advanced-url-features.mdreferences/api-components.mdreferences/api-functions.mdreferences/api-hooks.mdreferences/api-router-instance.mdreferences/api-types.mdreferences/config-bundlers.mdreferences/config-devtools.mdreferences/config-route-options.mdreferences/config-router-options.mdreferences/config-routing.mdreferences/config-virtual-routes.mdreferences/deployment-integrations.mdreferences/patterns-auth.mdreferences/patterns-data.mdreferences/patterns-links-blocking.mdreferences/patterns-params.mdreferences/testing-migration.mdreferences/troubleshooting.md

FAQ

What does the tanstack-router-guide skill do?

Type-safe, file-based router for React with first-class search params, data loading, and code splitting. Use when user asks to "create routes with TanStack Router", "set up file-based routing", "add search params", "use loaders", "protect routes with auth", "add code splitting", or asks about @tanstack/react-router, createFileRoute, createRouter, routeTree.gen.ts, useSearch, useParams, useNavigate, useBlocker, useMatch, useRouterState, beforeLoad, or route configuration. Do NOT use for TanStack Start server functions, Next.js App Router, React Router (without migration context), or Remix routing. Covers routing setup, navigation, search/path params, data loading, authentication, code splitting, SSR, error handling, testing, deployment, and bundler configuration (Vite, Webpack, Rspack, esbuild).

How do I install the tanstack-router-guide skill?

Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.

Does the tanstack-router-guide skill run scripts?

No, this skill is instructions only (SKILL.md) with no executable scripts.

Related skills

Compare tanstack-router-guide with