Skip to main content

Hooks API

Type-safe hooks for accessing router state and navigation.

useRouter

Access the current TanStack Router instance from React context.

Options

boolean
default:"true"
Log a warning if no router context is found.

Returns

Router
The registered router instance with methods like navigate, invalidate, preloadRoute, etc.

useNavigate

Imperative navigation hook.

Options

string
Optional route base used to resolve relative to paths.

Returns

Function
A stable function that accepts NavigateOptions:
  • to - Destination route path
  • params - Path parameters
  • search - Search parameters (object or updater function)
  • hash - Hash fragment
  • state - History state
  • replace - Replace history entry instead of push
  • resetScroll - Reset scroll position
  • viewTransition - Use View Transitions API

useParams

Access the current route’s path parameters with type-safety.

Options

string
The route path to get params from. Enables strict typing.
boolean
default:"true"
If true, only the route’s own params are returned. If false, all params from parent routes are included.
(params) => any
Project the params object to a derived value for memoized renders.
boolean
Enable structural sharing for stable references.

Returns

object
The params object (or selected value) for the matched route.

useSearch

Read and select the current route’s search parameters with type-safety.

Options

string
The route path to get search params from. Enables strict typing.
boolean
default:"true"
Control which route’s search is read and how strictly it’s typed.
(search) => any
Map the search object to a derived value for render optimization.
boolean
Enable structural sharing for stable references.

Returns

The search object (or selected value) for the matched route.

useLoaderData

Read and select the current route’s loader data with type-safety.

Options

string
The route path to get loader data from. Enables strict typing.
boolean
default:"true"
Choose which route’s data to read and strictness.
(data) => any
Map the loader data to a derived value.
boolean
Enable structural sharing for stable references.

Returns

any
The loader data (or selected value) for the matched route.

useLoaderDeps

Access the current route’s loader dependencies.

Options

string
The route path to get loader deps from.
boolean
default:"true"
Control strictness of typing.
(deps) => any
Map the deps to a derived value.

Returns

any
The loader dependencies for the matched route.

useMatch

Get the current route match data.

Options

string
The route path to match against.
boolean
default:"true"
Whether to enforce strict typing.
(match) => any
Select a derived value from the match.
boolean
Enable structural sharing for stable references.

Returns

RouteMatch
The route match object containing:
  • id - Unique match identifier
  • routeId - The route’s ID
  • pathname - The matched pathname
  • params - Path parameters
  • search - Search parameters
  • loaderData - Data from the loader
  • status - Match status (‘pending’ | ‘success’ | ‘error’)
  • And more…

useLocation

Get the current location object.

Returns

ParsedLocation
The current location object with:
  • href - The full URL path
  • pathname - The pathname
  • search - Parsed search parameters
  • searchStr - Raw search string
  • hash - The hash fragment
  • state - History state

useRouterState

Subscribe to router state with optional selection.

Options

(state) => any
Select a derived value from the router state.
boolean
Enable structural sharing for stable references.

Returns

RouterState | any
The router state (or selected value) containing:
  • status - Router status (‘pending’ | ‘idle’)
  • isLoading - Whether the router is loading
  • isTransitioning - Whether a navigation is in progress
  • matches - Current route matches
  • location - Current location
  • resolvedLocation - Resolved location after redirects

useRouteContext

Access the current route’s context.

Options

string
The route path to get context from.
boolean
default:"true"
Whether to enforce strict typing.
(context) => any
Select a derived value from the context.

Returns

any
The route context object.

useBlocker

Block navigation based on a condition.

Options

boolean
required
Whether to block navigation.
() => boolean | Promise<boolean>
required
A function that returns true to allow navigation or false to block it.

Returns

Void. The hook sets up the blocker internally.

useMatches

Get all current route matches.

Options

(matches) => any
Select a derived value from the matches array.
boolean
Enable structural sharing for stable references.

Returns

RouteMatch[]
Array of all current route matches from root to leaf.

useCanGoBack

Check if the router can go back in history.

Returns

boolean
Whether the router can navigate back in history.