Solid Router
TanStack Router provides first-class support for SolidJS with reactive primitives, components, and patterns designed specifically for Solid applications.Installation
Core Components
RouterProvider
The top-level component that renders active route matches and provides the router to the Solid tree via context.Link
Reactive navigation component with preloading and active state support.class instead of className (Solid convention).
Key Props:
to- Type-safe destination pathparams- Path parameterssearch- Search parametersactiveProps- Props applied when link is active (returns a function in Solid)inactiveProps- Props applied when link is inactivepreload- Controls preloading behavior (false,'intent','viewport')disabled- Disables navigation
Outlet
Renders child route matches at the current level.ErrorComponent
Default error boundary component that displays error details.Navigate
Component-based navigation trigger.Solid Primitives
useRouter
Access the router instance from any component.useNavigate
Type-safe programmatic navigation.useParams
Access route path parameters reactively.useParams returns a reactive proxy, not a signal.
useSearch
Access route search parameters reactively.useLoaderData
Access data loaded by the route’s loader function as a signal.useLoaderData returns a signal (function), so access data with post() instead of post.
useMatch
Access the current route match with reactive properties.useRouteContext
Access route context provided by parent routes reactively.useLocation
Access the current location object reactively.useRouterState
Access router state reactively with optional selector.isLoading().
useMatchRoute
Check if a specific route matches the current location.useLinkProps
Build anchor-like props for custom link components.useBlocker
Block navigation based on a condition (useful for unsaved changes).Route API
RouteApi
Route-specific API with pre-bound reactive primitives for a specific route ID.Creating Routes
Code-based Routes
File-based Routes
SSR Support
Solid Router includes specialized SSR utilities:Solid-Specific Features
Reactive Data Flow
All router state is fully reactive and integrates with Solid’s reactivity system:Signal-based Loader Data
Loader data is returned as Solid signals:Suspense Integration
Works seamlessly with Solid’s Suspense:Show/Match Components
Use Solid’s control flow components:Complete Example
Best Practices
-
Type Safety: Always register your router type:
-
Use Signals: Remember that
useLoaderDataanduseRouterStatereturn signals: -
Reactive Params:
useParamsanduseSearchreturn reactive proxies: -
Control Flow: Use Solid’s control flow components:
-
Class vs ClassName: Use
class(Solid convention) instead ofclassName:
Key Differences from React Router
- Signals:
useLoaderDataanduseRouterStatereturn signals, not plain values - Reactive Proxies:
useParamsanduseSearchreturn reactive proxies - Class Attribute: Use
classinstead ofclassName - Control Flow: Use
Show,For,Switch,Matchinstead of&&,map, ternaries - No Hooks Rules: Solid doesn’t have hook rules - use primitives anywhere