mcpField
T
tanstack-router

tanstack-router

github.com/oakossDevelopment21143 installsUpdated 12w ago
Repository

About tanstack-router

A skill for building type-safe, file-based routing in React applications with TanStack Router. It covers route data loading, validated search parameters, code splitting, and integration with TanStack Query.

Key features

  1. 01Define file-based React routes
  2. 02Load data at the route level
  3. 03Validate and type search parameters
  4. 04Split routes and configure preloading
  5. 05Integrate route loaders with TanStack Query

Use cases

  1. 01Create routes with typed parameters and search state
  2. 02Load page data without useEffect fetching
  3. 03Preload pages based on link intent or visibility

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/oakoss/agent-skills --skill tanstack-router

After installing, type/tanstack-routerin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

references/
auth-and-context.md
code-splitting.md
data-loading.md
known-issues.md
loader-query-patterns.md
navigation.md
search-params.md
setup.md
type-safety.md
virtual-routes.md

Details

From the project README

TanStack Router

Type-safe, file-based routing for React with route-level data loading, search params validation, code splitting, and TanStack Query integration.

Package: @tanstack/react-router | Plugin: @tanstack/router-plugin

Quick Reference

PatternUsage
createFileRoute('/path')Define file-based route
createRootRouteWithContext<T>()Root route with typed context
createLazyFileRoute('/path')Code-split route component
zodValidator(schema)Search params validation
Route.useLoaderData()Access loader data in component
Route.useParams()Type-safe route params
Route.useSearch()Type-safe search params
useNavigate()Programmatic navigation
useBlocker()Block navigation (dirty forms)
notFound()Throw 404 from loader
getRouteApi('/path')Type-safe hooks in split files
stripSearchParams(defaults)Clean default values from URLs
retainSearchParams(['key'])Preserve params across navs
useAwaited({ promise })Suspend until deferred promise resolves
useCanGoBack()Check if router can go back safely

Data Loading

MethodReturnsThrowsUse Case
ensureQueryDataDataYesRoute loaders (recommended)
prefetchQueryvoidNoBackground prefetching
fetchQueryDataYesImmediate data need
defer() (optional)PromiseNoStream non-critical data (promises auto-handled)

Preloading

StrategyBehaviorUse Case
'intent'Preload on hover/focusDefault for most links
'render'Preload when Link mountsCritical next pages
'viewport'Preload when Link in viewBelow-fold content
falseNo preloadingHeavy, rarely-visited pages

File Organization

FilePurpose
__root.tsxRoot route with <Outlet />
index.tsxIndex route for /
posts.$postId.tsxDynamic param route
_authenticated.tsxPathless layout (auth guard)
dashboard.lazy.tsxCode-split component

Common Mistakes

MistakeFix
Missing router type registrationAdd declare module with Register interface
useParams() without fromAlways pass from: '/route/path' for exact types
useNavigate() for regular linksUse <Link> for <a> tags, a11y, preloading
prefetchQuery in loadersUse ensureQueryData (returns data, throws errors)
Fetching in useEffectUse route loaders (prevents waterfalls)
Sequential fetches in loaderUse Promise.all for parallel requests
Missing leading slashAlways '/about' not 'about'
TanStackRouterVite after react()Plugin MUST come before react() in Vite config
strict: false params unparsedUse strict mode or manually parse after navigation
Pathless route notFoundComponentDefine notFoundComponent on child routes instead
Aborted loader undefined errorGuard errorComponent with if (!error) return null
No loaderDeps declaredDeclare deps so loader only re-runs when they change

Delegation

  • TanStack Query patterns — data fetching, caching, mutations: use tanstack-query skill
  • TanStack Start — server functions, SSR, server-side auth: use tanstack-start skill
  • TanStack Table — table rendering with router search params: use tanstack-table skill
  • Router + Query integration — loader data flow, preloading: see Loader Data Flow Patterns

If the tanstack-devtools skill is available, delegate router state debugging and route tree inspection to it.

References

  • Setup — installation, Vite config, file structure, app setup, router default options
  • Type Safety — register router, from param, strict: false, type utilities, getRouteApi
  • Data Loading — route loaders, Query integration, parallel loading, streaming, deferred data, abort signal, loaderDeps
  • Search Params — validation, strip/retain middleware, fine-grained subscriptions, debounce, custom serializers
  • Navigation — Link, active styling, relative navigation, hash, route masks, blocker, scroll restoration
  • Auth and Context — beforeLoad, context inheritance, pathless layouts, dependency injection, error handling
  • Code Splitting — lazy routes, auto splitting, preloading strategies, programmatic preloading
  • Virtual File Routes — rootRoute, route, index, layout, physical builders, mixing file-based and code-based routing
  • Known Issues — 20 documented issues with fixes, anti-patterns
  • Loader+Query Patterns — ensureQueryData in loaders, parallel loading, critical vs non-critical data, search-param-dependent loaders

Comments

Markdown supported