Skip to main content
TanStack Router provides a comprehensive set of hooks for accessing router state, navigation, and route information with full type safety.

Router Hooks

useRouter

Access the current TanStack Router instance from React context.
Source: packages/react-router/src/useRouter.tsx:16-25
{ warn?: boolean }
TRouter
The registered router instance.

useRouterState

Subscribe to the router’s state store with optional selection and structural sharing.
Source: packages/react-router/src/useRouterState.tsx:44-86
UseRouterStateOptions
TSelected | RouterState
The selected router state (or the full state by default).

useNavigate

Imperative navigation hook that returns a stable navigate function.
Source: packages/react-router/src/useNavigate.tsx:26-43
{ from?: string }
(options: NavigateOptions) => Promise<void>
A function that accepts NavigateOptions including:
  • to - Destination path
  • params - Path parameters
  • search - Search parameters
  • hash - URL hash
  • replace - Replace history entry
  • resetScroll - Reset scroll position

useLocation

Read the current location from the router state.
Source: packages/react-router/src/useLocation.tsx:40-52
UseLocationOptions
Location | TSelected
The current location object with properties:
  • pathname - Current path
  • search - Parsed search params
  • hash - URL hash
  • href - Full URL
  • state - Location state

Route Data Hooks

useParams

Access the current route’s path parameters with type safety.
Source: packages/react-router/src/useParams.tsx:76-107
UseParamsOptions
required
TParams | TSelected
The params object (or selected value) for the matched route.

useSearch

Read and select the current route’s search parameters with type safety.
Source: packages/react-router/src/useSearch.tsx:76-105
UseSearchOptions
required
TSearch | TSelected
The search object (or selected value) for the matched route.

useLoaderData

Read and select the current route’s loader data with type safety.
Source: packages/react-router/src/useLoaderData.tsx:68-91
UseLoaderDataOptions
required
TLoaderData | TSelected
The loader data (or selected value) for the matched route.

Match Hooks

useMatch

Read and select the nearest or targeted route match.
Source: packages/react-router/src/useMatch.tsx:82-123
UseMatchOptions
required
RouteMatch | TSelected
The route match object containing:
  • id - Match ID
  • routeId - Route ID
  • pathname - Matched pathname
  • params - Path parameters
  • search - Search parameters
  • loaderData - Loader data
  • context - Route context

useMatches

Read the full array of active route matches or select a derived subset.
Source: packages/react-router/src/Matches.tsx:233-250
UseMatchesOptions
RouteMatch[] | TSelected
The array of matches (or the selected value).

useMatchRoute

Create a matcher function for testing locations against route definitions.
Source: packages/react-router/src/Matches.tsx:144-174
(options: MatchRouteOptions) => false | TParams
A matchRoute function that returns false (no match) or the matched params object. Options include:
  • to - Route to match
  • params - Match specific params
  • search - Match specific search
  • fuzzy - Allow fuzzy matching
  • pending - Match against pending location
  • caseSensitive - Case-sensitive matching

Blocker Hook

useBlocker

Block navigation with custom logic and optional user confirmation.
Source: packages/react-router/src/useBlocker.tsx:131-260
UseBlockerOpts
required
void | BlockerResolver
When withResolver: true, returns:
  • status - ‘idle’ | ‘blocked’
  • current - Current location info
  • next - Next location info
  • action - History action
  • proceed() - Allow navigation
  • reset() - Cancel navigation

Usage Examples

Router State Selection

Imperative Navigation

Type-Safe Params

Search Params with Selection

Loader Data Access

Conditional Rendering with Match

Location Tracking

Performance Tips

Use Selectors

Always use the select option to narrow down your subscription:

Enable Structural Sharing

For complex selections, enable structural sharing:

Memoize Selectors

For expensive selections, memoize the selector:

See Also