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: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:
packages/solid-router/src/RouterProvider.tsx:1-47
<Link>
Creates type-safe navigation links with automatic active states:
packages/solid-router/src/link.tsx:605-646
<Outlet>
Renders child route components:
packages/solid-router/src/Match.tsx
Router Hooks
All hooks return reactive Solid signals for automatic updates:useRouter
Access the router instance:
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:packages/solid-router/src/route.tsx
Loader Context
Loaders receive context with params, search, and more:Error Handling
Handle errors with error components:packages/solid-router/src/CatchBoundary.tsx
Navigation
Programmatic Navigation
Link Preloading
Automatically preload routes on hover, focus, or when visible: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 thesrc/routes/ directory:
packages/solid-router/src/fileRoute.ts
Type Safety
Register your router for full type safety across your application:- 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: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:
| Solid Router | TanStack Router |
|---|---|
<Router> | <RouterProvider> |
<Routes> | Route tree with createRouter |
<Route> | createRoute |
<A> | <Link> |
useParams() | useParams() (returns signal) |
useSearchParams() | useSearch() |
useLocation() | useLocation() |
useNavigate() | useNavigate() |
API Reference
For detailed API documentation, see:- Router API - Core router APIs apply to Solid
- Route API - Route creation and configuration
- Link API - Navigation components
- Hooks API - Available hooks