Skip to main content

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.
Type-safe navigation component with preloading and active state support.
Key Props:
  • to - Type-safe destination path
  • params - Path parameters
  • search - Search parameters
  • activeProps - Props applied when link is active
  • inactiveProps - Props applied when link is inactive
  • preload - 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.
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

  1. Type Safety: Always register your router type for full type safety:
  2. Use RouteApi: For code-split routes, use getRouteApi instead of importing routes:
  3. Preloading: Configure preloading for better UX:
  4. Error Handling: Provide custom error components for better UX:
  5. Pending States: Use pending components for loading states: