Track router status
A component needs to react to router-wide state changes such as navigation status.
TanStack · Hook
A reactive hook for reading TanStack Router's internal state in a component. An optional selector can reduce the returned value to the specific state needed.
useRouterState subscribes a component to the router's internal state. Without options it returns the complete RouterState; with select, it derives a value from that state, and structuralSharing can control sharing for the selected result.
useRouterState(options?)| Name | Type | Description |
|---|---|---|
opts | UseRouterStateOptions<TSelected> | Optional selector and structural-sharing configuration. select receives RouterState and returns the selected value; structuralSharing controls sharing for that value. |
opts.select | (state: RouterState) => TSelected | Transforms RouterState into the value returned by the hook. |
opts.structuralSharing | boolean | Controls whether structural sharing is enabled for the selected result. |
The current RouterState, or TSelected when a select function is provided.
import { useRouterState } from '@tanstack/react-router'
export function RouterStatus() {
const status = useRouterState({
select: (state) => state.status,
})
return <output>{status}</output>
}This component subscribes to the router and renders only the selected navigation status.
A component needs to react to router-wide state changes such as navigation status.
A component needs one derived value rather than the complete RouterState object.
A component needs reactive router state instead of a direct, non-reactive router.state read.
It is a hook that exposes the router's current internal state to a component. It returns RouterState by default, or the value produced by select when a selector is supplied.
Follow-up: How would you avoid subscribing to more state than necessary? Pass select and return only the field or derived value the component needs.
The options object is optional. Its select callback receives RouterState and determines the returned value; structuralSharing configures whether structural sharing is enabled for that selected value.
Follow-up: What should a component use for current location data instead? Prefer useLocation, which is designed specifically for that access pattern.
router.state is current but non-reactive when accessed directly in a component, so a component will not re-render when it changes. useRouterState provides the reactive version.
Follow-up: When only route matches are needed, which specialized hook is preferable? Use useMatches rather than accessing the complete router state.
useLocationuseMatchesuseRouterRouterState