Access current active route object #159
-
I need to watch for route changes and access the currently active route object. I've found the following to watch for location updates: const router = useRouter()
useEffect(() => {
console.log(router.state.location)
}, [router.state.location]) I'm not sure if it's the right way to do so, and besides, I need the route object, so I should search the routes object which is quite expensive. Is there any better solution? And I think the documentation needs lots of improvements, most of the API and use cases are not covered yet. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
you can use the (for some reason undocumented) |
Beta Was this translation helpful? Give feedback.
-
That hook no longer exists. That's probably why it wasn't documented :P. I'm also wanting an answer to this question. How do you grab the current route? |
Beta Was this translation helpful? Give feedback.
-
I am also searching for a way to grab the currently active route, |
Beta Was this translation helpful? Give feedback.
-
For the current active route state you have to use // ./src/routes/activity.lazy.tsx
import { createLazyFileRoute, useRouterState } from "@tanstack/react-router";
export const Route = createLazyFileRoute("/activity")({
component: ActivityPage,
});
function ActivityPage() {
const state = useRouterState();
} |
Beta Was this translation helpful? Give feedback.
I'd recommend using a more fine-grained selector to reduce the number of component re-renders.