I am trying react router v6. As per react training blog, I have created object of routing and passed to useRoutes()
:
function SomeOtherElement() {
return <h1>add user</h1>;
}
const routes = [
{
path: 'app',
element: <DashboardLayout />,
children: [
{ path: 'account', element: <AccountView /> },
{
path: 'users', element: <UserListView />,
children: [
{ path: 'add', element: <SomeOtherElement /> }
]
},
{ path: 'dashboard', element: <DashboardView /> },
{ path: 'products', element: <ProductListView /> },
{ path: 'settings', element: <SettingsView /> },
{ path: '*', element: <Navigate to="/404" /> }
]
}];
const routing = useRoutes(routes);
But the nested routing is not working. As you can see in above object, I want to create URL and render the UI for user "add" functionality.
URL in the browser is getting updated correctly to http://localhost:3000/app/users/add but UI is not updating.
The default behavior of React Router fails to render multiple child routes with single Outlet. For example - When there is requirement to have a single Header in all the pages and replace the common content (Single Outlet) based on Route.
The trick here is to not provide any element attribute for parent but to provide this component as a child route with the help of Index attribute.
Index helps us to have a default component for a parent route if the child routes doesn't match any of the child routes.
The code snippet for this would be similar to the one provided by Samira.
<BrowserRouter>
<Routes>
<Route path="/" element={<App />}>
<Route path="" element={<Home />}></Route>
<Route path="child1"> //no element attribute here
<Route index={true} element={<Child1 />}></Route>
<Route path="grandChild1" element={<GrandChild1 />}></Route>
<Route path="grandChild2" element={<GrandChild2 />}></Route>
</Route>
</Route>
</Routes>
</BrowserRouter>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With