How to Access the Full Path of a Route (Including Pathless Routes) #3242
Unanswered
bilal-dev-bit
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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.
why do i want to get the full path with patheless route included?
answer: i want to display different component for different url instead for example i have _auth/dashboard route and i want to add sidebar if the route is under _auth how can do that
export const Route = createRootRouteWithContext<{
queryClient: QueryClient
}>()({
component: RootLayout,
})
function RootLayout() {
const location = useLocation()
const sidebarPaths = [
'/dashboard',
'/donors',
'/donors/$id', // Add dynamic route for donors
];
// 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={${show_sidebar ? "flex" : ""}}>
{
show_sidebar
?
:
}this way seems bad but if i can get the current path that start with _auth that would be simple
Beta Was this translation helpful? Give feedback.
All reactions