本文へ移動
cccskills
無料GitHub で公開

tanstack-router

TanStack Router best practices for type-safe routing, file-based routing, data loading, search params, and navigation in React. Use when building React SPAs with complex routing, implementing type-safe search params, setting up route loaders, integrating with TanStack Query, or configuring code splitting and preloading.

インストール方法を見る

含まれるファイル(12)

  • SKILL.md6.7 KB
  • rules/ctx-route-context.md3.5 KB
  • rules/err-error-handling.md2.4 KB
  • rules/load-data-loading.md3.3 KB
  • rules/load-query-integration.md3.5 KB
  • rules/nav-navigation.md2.9 KB
  • rules/org-file-based-routing.md4.4 KB
  • rules/pre-preloading.md2.0 KB
  • rules/router-configuration.md2.2 KB
  • rules/search-params.md5.1 KB
  • rules/split-code-splitting.md2.3 KB
  • rules/ts-type-safety.md3.3 KB

SKILL.md(原文)

インストールする前に、エージェントに与えられる指示の中身を確認できます。

TanStack Router

Version: @tanstack/react-router@1.x Requires: React 18.0+, TypeScript 5.0+, Vite (recommended)

Quick Setup

npm install @tanstack/react-router @tanstack/react-router-devtools
npm install -D @tanstack/router-plugin

Vite Plugin

// 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(), react()],
})

Root Route

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

export const Route = createRootRoute({
  component: () => (
    <>
      <Outlet />
      <TanStackRouterDevtools />
    </>
  ),
})

Unified Devtools (Recommended with Multiple TanStack Libraries)

If using Router + Query (or other TanStack libraries), use the unified TanStackDevtools shell instead of individual devtools components:

npm install -D @tanstack/react-devtools
// src/routes/__root.tsx
import { createRootRoute, Outlet } from '@tanstack/react-router'
import { TanStackRouterDevtoolsPanel } from '@tanstack/react-router-devtools'
import { TanStackDevtools } from '@tanstack/react-devtools'

export const Route = createRootRoute({
  component: () => (
    <>
      <Outlet />
      <TanStackDevtools
        config={{ position: 'bottom-right' }}
        plugins={[
          { name: 'TanStack Router', render: <TanStackRouterDevtoolsPanel /> },
          // Add more plugins: Query, etc.
        ]}
      />
    </>
  ),
})

Use *Panel variants (TanStackRouterDevtoolsPanel, ReactQueryDevtoolsPanel) when embedding inside TanStackDevtools.

Router Creation & Type Registration

// src/router.tsx
import { createRouter } from '@tanstack/react-router'
import { routeTree } from './routeTree.gen'

export const router = createRouter({ routeTree })

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

Entry Point

// src/main.tsx
import { RouterProvider } from '@tanstack/react-router'
import { router } from './router'

function App() {
  return <RouterProvider router={router} />
}

File Structure

src/routes/
├── __root.tsx              # Root layout (always rendered)
├── index.tsx               # "/" route
├── about.tsx               # "/about" route
├── posts.tsx               # "/posts" layout
├── posts.index.tsx         # "/posts" index
├── posts.$postId.tsx       # "/posts/:postId" dynamic route
├── _auth.tsx               # Pathless layout (auth guard)
├── _auth.dashboard.tsx     # "/dashboard" (wrapped by _auth)
└── (settings)/
    ├── settings.tsx         # Route group
    └── settings.profile.tsx

Rule Categories

PriorityCategoryRule FileImpact
CRITICALType Safetyrules/ts-type-safety.mdPrevents runtime errors, enables refactoring
CRITICALFile-Based Routingrules/org-file-based-routing.mdEnsures maintainable route structure
HIGHRouter Configrules/router-configuration.mdGlobal defaults for preload, scroll, errors
HIGHData Loadingrules/load-data-loading.mdOptimizes data fetching, prevents waterfalls
HIGHQuery Integrationrules/load-query-integration.mdTanStack Query + Router wiring
HIGHSearch Paramsrules/search-params.mdType-safe URL state management
HIGHError Handlingrules/err-error-handling.mdGraceful error and 404 handling
MEDIUMNavigationrules/nav-navigation.mdType-safe links and programmatic nav
MEDIUMCode Splittingrules/split-code-splitting.mdReduces bundle size
MEDIUMPreloadingrules/pre-preloading.mdImproves perceived performance
LOWRoute Contextrules/ctx-route-context.mdDependency injection and auth guards

Critical Rules

Always Do

  • Register router type — declare module @tanstack/react-router with Register.router for global type inference
  • Use from parameter in hooks (useSearch, useParams, useLoaderData) to get exact types for the current route
  • Validate search params — use validateSearch with any Standard Schema library (Zod, Valibot, Yup, ArkType, etc.)
  • Use file-based routing — let the plugin generate the route tree, don't maintain it manually
  • Use loaders for data — fetch in loader, not in components (prevents waterfalls, enables preloading)

Never Do

  • Don't skip type registration — without it, all hooks return unknown unions
  • Don't fetch data in useEffect — use loader or beforeLoad instead
  • Don't use string paths without Link's type checking — <Link to="/typo"> catches errors at compile time
  • Don't put heavy logic in components — loaders run before render and enable preloading/parallel fetching

Key Patterns

// Auth guard with beforeLoad + redirect
export const Route = createFileRoute('/_auth')({
  beforeLoad: ({ context }) => {
    if (!context.auth.user) {
      throw redirect({ to: '/login', search: { redirect: location.href } })
    }
  },
})

// Search params with Standard Schema (no adapter needed)
import { z } from 'zod'

const searchSchema = z.object({
  page: z.number().default(1),
  sort: z.enum(['newest', 'oldest']).default('newest'),
})

export const Route = createFileRoute('/posts')({
  validateSearch: searchSchema, // Pass schema directly
})

// Loader with ensureQueryData
export const Route = createFileRoute('/posts/$postId')({
  loader: ({ context, params }) =>
    context.queryClient.ensureQueryData(postQueryOptions(params.postId)),
  component: PostComponent,
})

function PostComponent() {
  const post = Route.useLoaderData()
  return <h1>{post.title}</h1>
}

// Code-split with .lazy.tsx
// posts.tsx — keeps loader (critical path)
export const Route = createFileRoute('/posts')({
  loader: () => fetchPosts(),
})

// posts.lazy.tsx — splits component (loaded after)
export const Route = createLazyFileRoute('/posts')({
  component: PostsComponent,
})

レビュー

まだレビューはありません。使ってみた感想をお寄せください。

同じリポジトリのスキル

概要と使いどころ

Implement web accessibility (a11y) best practices following WCAG guidelines to create inclusive, accessible user interfaces.

日本語の概要は準備中です。原文の説明を表示しています。

fellipeutaka/kanpeki332026年8月28日 更新

Browser automation CLI for AI agents. Use when the user needs to interact with websites, including navigating pages, filling forms, clicking buttons, taking screenshots, extracting data, testing web apps, or automating any browser task. Triggers include requests to "open a website", "fill out a form", "click a button", "take a screenshot", "scrape data from a page", "test this web app", "login to a site", "automate browser actions", or any task requiring programmatic web interaction.

日本語の概要は準備中です。原文の説明を表示しています。

fellipeutaka/kanpeki332026年8月28日 更新

bun

無料

Bun runtime, package manager, bundler, and test runner. Use when running scripts with bun, managing packages, serving HTTP with Bun.serve, querying databases with Bun.sql/bun:sqlite/Bun.redis, shell scripting with $, using S3/file I/O, writing tests with bun:test, bundling or compiling to executable, or using any Bun-specific API (spawn, glob, semver, FFI, workers, plugins, HTMLRewriter).

日本語の概要は準備中です。原文の説明を表示しています。

fellipeutaka/kanpeki332026年8月28日 更新

Create high-quality git commits: review/stage intended changes, split into logical commits, and write clear commit messages (including Conventional Commits). Use when the user asks to commit, craft a commit message, stage changes, or split work into multiple commits.

日本語の概要は準備中です。原文の説明を表示しています。

fellipeutaka/kanpeki332026年8月28日 更新

denji

無料

Manage SVG icons as framework components using Denji CLI. Use when the user needs to add, remove, list, or manage SVG icons in React, Preact, Solid, Qwik, Vue, or Svelte projects. Triggers include requests to "add an icon", "set up icons", "manage SVG icons", "remove an icon", "list icons", or any task involving Iconify icons as framework components.

日本語の概要は準備中です。原文の説明を表示しています。

fellipeutaka/kanpeki332026年8月28日 更新

Create distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.

日本語の概要は準備中です。原文の説明を表示しています。

fellipeutaka/kanpeki332026年8月28日 更新

fellipeutaka のスキルをすべて見る

このスキルの問題を報告する