T
TanStack8mo ago
like-gold

How to Access the Full Path of a Route (Including Pathless Routes)

I’m trying to access the full path of a route in my application, including cases where some parent routes are pathless (e.g., _auth/dashboard). From my understanding, there should be a way to retrieve the full path of the current route, but I couldn’t find any relevant methods or hooks in the official documentation. What I’ve Tried: I looked for a useRoute hook or a getFullPath method in the documentation, but these do not seem to exist in the latest version. I explored hooks like useParams and useSearch,, but these do not provide the full path of the route, especially when dealing with pathless routes.
3 Replies
equal-jade
equal-jade8mo ago
Route.fullPath ?
like-gold
like-goldOP8mo ago
no i want to acess the current path that start with _auth to show some component here the solution i came up with export const Route = createRootRouteWithContext<{ queryClient: QueryClient }>()({ component: RootLayout, }) function RootLayout() { const location = useLocation() const sidebarPaths = [ '/dashboard', '/donors', '/donors/$id', // Add dynamic route for donors '/blood-inventory', '/blood-inventory/$id', '/blood-inventory/new', '/health-institute', '/health-institute/add', '/blood-request', ]; // Check if the current path matches any of the sidebar paths const show_sidebar = sidebarPaths.some((path) => { const pathPattern = new RegExp(^${path.replace(/\$id/g, '[^/]+')}$); return pathPattern.test(location.pathname); }); function logOut() { router.invalidate(); } return ( <React.Fragment> <div className="text-white"> <div className={${show_sidebar ? "flex" : ""}}> { show_sidebar ? <SideBar router={logOut}/> : <NavBar/> } .....) this way seems bad but if i can get the current path that start with _auth that would be simple is there a better way to do what i did
equal-jade
equal-jade8mo ago
should be doable via useMatch

Did you find this page helpful?