I have a react project where I make use of React Router DOM to render different pages based on the visited URL.
All of the components render properly when I navigate to their respective URLs, except the Account component (when navigating to /admin/dashboard/table).
My routes are:
<Route path="/admin" element={<AdminLayout />}>
<Route index path="home" element={<AdminHome />} />
<Route path="dashboard" element={<Dashboard />}>
<Route path="table" element={<Account />} />
</Route>
</Route>
The Link to the /admin/dashboard/table URL is here:
<CardActionArea component={Link} to="table">
<CardMedia
component="img"
height="300"
image={cardImage}
alt="green iguana"
/>
When clicking on the link, I successfully navigate to /admin/dashboard/table, but I do not understand why the Account component is not rendering when navigating to the URL.
2
Answers
I found my error.
The Account page rendered properly, but because I nested it under the dashboard Route, it was not visible, due to the Dashboard component being on top of it.
I just had to edit my Routes as follows:
If you mean for the
Account
component to be rendered on nested route"/admin/dashboard/table"
then theDashBoard
component necessarily needs to render anOutlet
component for the nested routes similar to whatAdminLayout
does.Example:
This of course means that the
Dashboard
UI will be rendered at the same time as any of its nested route content.If you meant for the
Account
component to be rendered discretely on it’s own then you’ll not want to nest it as a child ofDashboard
, but rather as a sibling route.Examples: