How to handle nested routes with (vite and) react-router? #4501
-
|
I'm just wondering how one is supposed to handle nested routing when using the toolpad. Currently, on the tutorial page, simple routing ( If one needs to have routes such as What I've done is:
const router = createBrowserRouter(
[
{
Component: App,
children: [
{
path: "/",
Component: Layout,
children: [
{
path: "/",
Component: DashboardPage,
},
{
path: "/orders",
children: [
{
path: "",
Component: OrdersPage,
},
{
path: ":id",
Component: OrderPage,
},
],
},
],
},
],
},
],
);
<AppProvider navigation={NAVIGATION} theme={theme} branding={BRANDING}>
<Outlet />
</AppProvider>
<DashboardLayout>
<PageContainer>
<Outlet />
</PageContainer>
</DashboardLayout>With the above approach I had to:
{
segment: "orders",
title: "Orders",
pattern: "orders{/:id}*",
},
I'm not sure if the approach makes sense or if I should have rendered another I'm also facing other challenges such as the page container toolbar which needs to be different depending on the route. I'm also using path matching and rendering different toolbars based on the path. Is that the recommended approach in such cases? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
Hi! From what you describe your approach seems pretty much correct, we've been expanding on some examples gradually and have been running into the same types of use cases / scenarios. For example, this is a router configuration I've added while setting up individual pages for orders: const router = createBrowserRouter([
{
Component: App,
children: [
{
path: '/',
Component: Layout,
children: [
{
path: '',
Component: DashboardPage,
},
{
path: 'orders',
Component: OrdersPage,
},
{
path: 'orders/:orderId',
Component: OrderPage,
},
],
},
],
},
]);and navigation: const NAVIGATION: Navigation = [
{
kind: 'header',
title: 'Main items',
},
{
title: 'Dashboard',
icon: <DashboardIcon />,
},
{
segment: 'orders',
title: 'Orders',
icon: <ShoppingCartIcon />,
pattern: 'orders{/:orderId}?',
},
];For a custom title in the breadcrumbs, the best approach seems to be to check the path from the React Router hooks to set some conditional logic, yes. Let us know if you have any ideas on how that could be better! There should be no need for more For the page container toolbar, the same approach as for customizing the title and breadcrumbs according to the current path seems to be the best way to do it. Again if you think that could be improved somehow let us know! |
Beta Was this translation helpful? Give feedback.
Here's a quick simple CodeSandbox example if it helps:
https://codesandbox.io/p/devbox/crazy-https-66g4c5?workspaceId=ws_DRKKUErDGVJgP35ujtPhj9