Skip to main content

TanStack Solid Router

TanStack Router provides first-class support for SolidJS with a reactive, type-safe routing solution. It integrates seamlessly with Solid’s fine-grained reactivity system and provides built-in caching, preloading, and URL state management.

Installation

Install the Solid Router package:

Quick Start

Here’s a minimal example to get started with TanStack Router in a SolidJS application:
Source: packages/solid-router/src/router.ts:76-78

Core Concepts

Reactive Data with Signals

Solid Router integrates with SolidJS’s reactive primitives. Data returned from hooks are wrapped in signals for automatic reactivity:

Router Components

Solid Router provides reactive components that work with Solid’s JSX:

<RouterProvider>

The top-level component that provides the router to your application:
Source: packages/solid-router/src/RouterProvider.tsx:1-47

<Link>

Creates type-safe navigation links with automatic active states:
Source: packages/solid-router/src/link.tsx:605-646

<Outlet>

Renders child route components:
Source: packages/solid-router/src/Match.tsx

Router Hooks

All hooks return reactive Solid signals for automatic updates:

useRouter

Access the router instance:
Source: packages/solid-router/src/useRouter.tsx:6-15

useNavigate

Get a type-safe navigate function:

useParams

Access route parameters:

useSearch

Access search parameters:

useLoaderData

Access loader data for the current route:

useRouterState

Access reactive router state:

Data Loading

Route Loaders

Define data loading at the route level:
Source: packages/solid-router/src/route.tsx

Loader Context

Loaders receive context with params, search, and more:

Error Handling

Handle errors with error components:
Source: packages/solid-router/src/CatchBoundary.tsx

Programmatic Navigation

Automatically preload routes on hover, focus, or when visible:
Source: packages/solid-router/src/link.tsx:200-207

File-Based Routing

TanStack Router supports file-based routing for SolidJS. Use the Vite plugin to automatically generate routes:

Setup

Route Files

Create routes in the src/routes/ directory:
Source: packages/solid-router/src/fileRoute.ts

Type Safety

Register your router for full type safety across your application:
This enables:
  • Autocomplete for route paths
  • Type-safe params and search parameters
  • Type-safe loader data
  • Type-safe navigation

SSR Support

TanStack Solid Router supports server-side rendering:
For full SSR support, consider using TanStack Start, the full-stack framework built on TanStack Router.

DevTools

Add the TanStack Router DevTools to inspect routing state during development:

Advanced Features

Search Parameter Validation

Validate and parse search parameters with schemas:

Route Context

Share data between parent and child routes:

Lazy Loading

Lazy load route components for code splitting:

Migration from Solid Router

If you’re migrating from @solidjs/router, here are the key differences:

API Reference

For detailed API documentation, see:

Examples

Explore example applications in the repository:

Resources