React Router
TanStack Router provides first-class support for React with type-safe components, hooks, and patterns designed specifically for React applications.Installation
Core Components
RouterProvider
The top-level component that renders active route matches and provides the router to the React tree via context.Link
Type-safe navigation component with preloading and active state support.to- Type-safe destination pathparams- Path parameterssearch- Search parametersactiveProps- Props applied when link is activeinactiveProps- 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.React Hooks
useRouter
Access the router instance from any component.useNavigate
Type-safe programmatic navigation.useParams
Access route path parameters with type safety.useSearch
Access route search parameters with type safety.useLoaderData
Access data loaded by the route’s loader function.useMatch
Access the current route match with full type safety.useRouteContext
Access route context provided by parent routes.useLocation
Access the current location object.useRouterState
Access router state with optional selector.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 hooks for a specific route ID.Creating Routes
Code-based Routes
File-based Routes
SSR Support
React Router includes specialized SSR utilities:React-Specific Features
Error Boundaries
React Router integrates with React’s error boundaries:Suspense Integration
Works seamlessly with React Suspense:React 19 Support
Fully compatible with React 19 features including:- Server Components (RSC)
- Server Actions
- Enhanced hydration
- View Transitions
Complete Example
Best Practices
-
Type Safety: Always register your router type for full type safety:
-
Use RouteApi: For code-split routes, use
getRouteApiinstead of importing routes: -
Preloading: Configure preloading for better UX:
-
Error Handling: Provide custom error components for better UX:
-
Pending States: Use pending components for loading states: