Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Reuse the same user routes under '/' (UserLayout) and '/admin' (AdminLayout) without duplication

I need to define user pages (Home, Cows, Reports, ...) once and make them accessible to AppUser + Admin under "/" with UserLayout, and also accessible to Admin under "/admin/user/*" but rendered inside AdminLayout. When I try to reuse the same route array in both places, I sometimes get UserLayout showing for "/admin/user/" or matching conflicts due to absolute vs relative paths. I'm looking for an idiomatic pattern to mount the same relative child routes under two parents (two layouts) without duplication and without layout leaks.

I mounted user pages only under "/" inside UserLayout, guarded for both roles (AppUser + Admin). I mounted admin-only pages under "/admin/*" inside AdminLayout, guarded for Admin only.

Router (what I did):

router.jsx (essentials)

{
 element: <RequireAuth />,
 children: [
 {
 path: "/",
 element: <UserLayout />,
 children: [
 {
 element: <RequireRole allowed={[Roles.AppUser, Roles.Admin]} />,
 children: [
 { path: "/", element: <Home /> },
 { path: "/cows", element: <AnimalManagement /> },
 // ...other user pages under "/"
 ],
 },
 ],
 },
 {
 path: "/admin",
 element: <RequireRole allowed={[Roles.Admin]} />,
 children: [
 {
 path: "/admin",
 element: <AdminLayout />,
 children: [
 { path: "/admin/dashboard", element: <AdminDashboard /> },
 // ...other admin-only pages under "/admin"
 ],
 },
 ],
 },
 ],
}

Expectation:

When an Admin clicks user pages from the admin sidebar, I want those pages to render inside AdminLayout (so the admin shell/Sidebar stays visible), even though the same pages are also available at "/" for both roles.

Actual result:

Because user pages exist only under "/", navigating to them shows UserLayout, not AdminLayout. I’m looking for an React-Router/React-Router-DOM v6 pattern to mount the same user routes under "/admin/user/*" (alias) so they render inside AdminLayoutwithout duplicating the route definitions or breaking role guards.

Answer*

Draft saved
Draft discarded
Cancel

lang-js

AltStyle によって変換されたページ (->オリジナル) /