I am using react-router-dom V6
both Routes
and useRoute
, So, in the site, the main routes are as follow:
JavaScript
x
12
12
1
export default function App() {
2
return (
3
4
<Routes>
5
<Route path="/" element={<Components />} />
6
<Route path="admin" element={<AdminApp />} />
7
</Routes>
8
9
10
)
11
}
12
As see above the second Route’s element is <AdminApp />
this element is coming from the following:
JavaScript
1
29
29
1
export default function AdminApp() {
2
return useRoutes([
3
{
4
path: '/dashboard',
5
element: <DashboardLayout />,
6
children: [
7
{ path: '/', element: <Navigate to="/dashboard/app" replace /> },
8
{ path: 'app', element: <DashboardApp /> },
9
{ path: 'user', element: <User /> },
10
{ path: 'products', element: <Products /> },
11
{ path: 'blog', element: <Blog /> }
12
]
13
},
14
{
15
path: '/',
16
element: <LogoOnlyLayout />,
17
children: [
18
{ path: 'login', element: <Login /> },
19
{ path: 'register', element: <Register /> },
20
{ path: '404', element: <NotFound /> },
21
{ path: '/', element: <Navigate to="/dashboard" /> },
22
{ path: '*', element: <Navigate to="/404" /> }
23
]
24
},
25
26
{ path: '*', element: <Navigate to="/404" replace /> }
27
]);
28
}
29
So whenever i call the route /admin
nothing display in the screen and in the console log
I am getting this warning
JavaScript
1
4
1
index.tsx:90 You rendered descendant <Routes> (or called `useRoutes`) at "/admin" (under <Route path="admin">) but the parent route path has no trailing "*". This means if you navigate deeper, the parent won't match anymore and therefore the child routes will never render.
2
3
Please change the parent <Route path="admin"> to <Route path="admin/*">
4
I have changed <Route path="admin" element={<AdminApp />} />
to <Route path="admin/*" element={<AdminApp />} />
so the warning disappeared but the page displays blank when i navigate to the route admin/
and it replace admin
word with dashboard
in the url:
what happened:
JavaScript
1
2
1
http://localhost:3000/admin => http://localhost:3000/dashboard
2
what expected:
JavaScript
1
2
1
http://localhost:3000/admin => http://localhost:3000/dashboard/app
2
Advertisement
Answer
I have replaced ‘/’ in first child route to ” and it worked for me. something like this :
JavaScript
1
11
11
1
return useRoutes([
2
{
3
path: "dashboard",
4
children: [
5
{ path: "", element: <Navigate to="dashboard/app" replace /> },
6
{ path: "app", element: <DashboardApp /> },
7
{ path: "user", element: <User /> }
8
]
9
}
10
]);
11
Here is simple app working example