The useRouter method is a hook that returns the current instance of Router from context. This hook is useful for accessing the router instance in a component.
⚠️⚠️⚠️ router.state is always up to date, but NOT REACTIVE. If you use router.state in a component, the component will not re-render when the router state changes. To get a reactive version of the router state, use the useRouterState hook.
import { useRouter } from '@tanstack/react-router'
function Component() {
const router = useRouter()
// ^ Router
// ...
}
import { useRouter } from '@tanstack/react-router'
function Component() {
const router = useRouter()
// ^ Router
// ...
}
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.