The useRouterState method is a hook that returns the current internal state of the router. This hook is useful for accessing the current state of the router in a component.
Tip
If you want to access the current location or the current matches, you should try out the useLocation and useMatches hooks first. These hooks are designed to be more ergonomic and easier to use than accessing the router state directly.
The useRouterState hook accepts an optional options object.
import { useRouterState } from '@tanstack/react-router'
function Component() {
const state = useRouterState()
// ^ RouterState
// OR
const selected = useRouterState({
select: (state) => state.location,
})
// ^ ParsedLocation
// ...
}
import { useRouterState } from '@tanstack/react-router'
function Component() {
const state = useRouterState()
// ^ RouterState
// OR
const selected = useRouterState({
select: (state) => state.location,
})
// ^ ParsedLocation
// ...
}
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.