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 AdminLayout—without duplicating the route definitions or breaking role guards.