admin管理员组文章数量:1202803
I'm using the Dashboard Layout component of Material UI's Toolpad Core. It works well. It shows all the icons on the slideout and correctly navigates to the page when clicked. It uses BrowserRouter in the main page (app/(dashboard)/layout.tsx).
One of those pages contains a MUI Select component. I want to navigate to one of another set of sub-pages from there, depending on what is selected. Unfortunately, That doesn't work. Nothing happens.
I don't want to add the sub-pages to the main navigation because it makes too many icons. I know I could fold them in under a sub-navigation of one of the items, but I'd rather not. It gets too long and busy. I prefer the Select component. But if I add another router on that page, the compiler complains that there can be only one router in the application. I can't figure away around that restriction.
Here's some code to clarify.
app>(dashboard)>layout.tsx
const NAVIGATION: Navigation = [{
segment: 'diet',
title: 'Diet',
icon: <DietIcon />,
},
{
segment: 'exercise',
title: 'Exercise',
icon: <ExerciseIcon />,
},
...}]
diet is the page that contains the Select component.
export default function RootLayout(props: any) {
return (
<html>
<body>
<BrowserRouter>
<AppProvider
navigation={NAVIGATION}
>
{props.children}
</AppProvider>
</BrowserRouter>
</body>
</html>
);
}
Clicking on the Diet icon takes you to
app>(dashboard)>diet/page.tsx
export default function DietPage() {
const [page, setPage] = React.useState('');
const navigate = useNavigate();
const handleChange = (event: SelectChangeEvent) => {
const selectedPage = event.target.value;
console.log("selectedPage=", selectedPage)
navigate(`./${selectedPage}`); // Navigate to the selected page
setPage(event.target.value as string);
};
return (
<table>
<tbody><tr>
<td><h2>Diet</h2></td>
<td>
<FormControl sx={{ m: 1, minWidth: 120 }}>
<InputLabel>Select Food Category</InputLabel>
<Select
value={page}
labelId="demo-simple-select-label"
id="demo-simple-select"
label="Select Food Category"
sx={{ width: 200 }}
onChange={handleChange}
>
<MenuItem value={"meals"}>
<div style={{ display: 'flex', alignItems: 'center' }}>
<MealIcon fontSize="small" />
<div>Meals</div>
</div>
</MenuItem>
<MenuItem value={"calendar"}>
<div style={{ display: 'flex', alignItems: 'center' }}>
<CalendarIcon fontSize="small" />
<div>Calendar</div>
</div>
</MenuItem>
</Select>
</FormControl>
</td>
</tr></tbody>
</table>
When you select Meals from the dropdown, it's supposed to navigate to meals.tsx, but it doesn't.
app>(dashboard)>diet>meals.tsx
export default function MealsPage() {
return (
<h2>
Meals Page
</h2>
);
}
I also tried first answer suggestion so that now the BrowserRouter looks like
<BrowserRouter>
<AppProvider
navigation={NAVIGATION}
branding={BRANDING}
theme={theme}
>
{props.children}
</AppProvider>
<Routes>
<Route path="diet" element={< DietPage />} />
<Route path="meals" element={<MealsPage />} />
</Routes>
</BrowserRouter>
But it still does nothing.
I think the problem has something to do with the next.js framework as asserted in next.js router issue but I can't get that to work either.
I'm using the Dashboard Layout component of Material UI's Toolpad Core. It works well. It shows all the icons on the slideout and correctly navigates to the page when clicked. It uses BrowserRouter in the main page (app/(dashboard)/layout.tsx).
One of those pages contains a MUI Select component. I want to navigate to one of another set of sub-pages from there, depending on what is selected. Unfortunately, That doesn't work. Nothing happens.
I don't want to add the sub-pages to the main navigation because it makes too many icons. I know I could fold them in under a sub-navigation of one of the items, but I'd rather not. It gets too long and busy. I prefer the Select component. But if I add another router on that page, the compiler complains that there can be only one router in the application. I can't figure away around that restriction.
Here's some code to clarify.
app>(dashboard)>layout.tsx
const NAVIGATION: Navigation = [{
segment: 'diet',
title: 'Diet',
icon: <DietIcon />,
},
{
segment: 'exercise',
title: 'Exercise',
icon: <ExerciseIcon />,
},
...}]
diet is the page that contains the Select component.
export default function RootLayout(props: any) {
return (
<html>
<body>
<BrowserRouter>
<AppProvider
navigation={NAVIGATION}
>
{props.children}
</AppProvider>
</BrowserRouter>
</body>
</html>
);
}
Clicking on the Diet icon takes you to
app>(dashboard)>diet/page.tsx
export default function DietPage() {
const [page, setPage] = React.useState('');
const navigate = useNavigate();
const handleChange = (event: SelectChangeEvent) => {
const selectedPage = event.target.value;
console.log("selectedPage=", selectedPage)
navigate(`./${selectedPage}`); // Navigate to the selected page
setPage(event.target.value as string);
};
return (
<table>
<tbody><tr>
<td><h2>Diet</h2></td>
<td>
<FormControl sx={{ m: 1, minWidth: 120 }}>
<InputLabel>Select Food Category</InputLabel>
<Select
value={page}
labelId="demo-simple-select-label"
id="demo-simple-select"
label="Select Food Category"
sx={{ width: 200 }}
onChange={handleChange}
>
<MenuItem value={"meals"}>
<div style={{ display: 'flex', alignItems: 'center' }}>
<MealIcon fontSize="small" />
<div>Meals</div>
</div>
</MenuItem>
<MenuItem value={"calendar"}>
<div style={{ display: 'flex', alignItems: 'center' }}>
<CalendarIcon fontSize="small" />
<div>Calendar</div>
</div>
</MenuItem>
</Select>
</FormControl>
</td>
</tr></tbody>
</table>
When you select Meals from the dropdown, it's supposed to navigate to meals.tsx, but it doesn't.
app>(dashboard)>diet>meals.tsx
export default function MealsPage() {
return (
<h2>
Meals Page
</h2>
);
}
I also tried first answer suggestion so that now the BrowserRouter looks like
<BrowserRouter>
<AppProvider
navigation={NAVIGATION}
branding={BRANDING}
theme={theme}
>
{props.children}
</AppProvider>
<Routes>
<Route path="diet" element={< DietPage />} />
<Route path="meals" element={<MealsPage />} />
</Routes>
</BrowserRouter>
But it still does nothing.
I think the problem has something to do with the next.js framework as asserted in next.js router issue but I can't get that to work either.
Share Improve this question edited Jan 25 at 4:13 tekknow asked Jan 22 at 0:56 tekknowtekknow 679 bronze badges 3 |2 Answers
Reset to default 1 +50I don't know particular versions of MUI library and react router (v6?). But based on what I see you might have misconfigured your router configuration. You must've had routes section for the Browser router:
<BrowserRouter>
<Routes>
<Route path="diet" element={< DietPage />} />
<Route path="meals" element={<MealsPage />} />
</Routes>
...
</BrowserRouter>
NAVIGATION
object doesn't provide router configuration. It provides elements for the left navigation menu.
Also MUI version should be compatible with react router version, because different versions might have different ways to specify routes.
Keep in mind that the current MUI website example is using v7 router as it's base.
Edit:
You need to create router somewhere (call createBrowserRouter for v7) and provide this info to MUI.
This might be done with AppProvider
router prop:
<AppProvider
navigation={NAVIGATION}
router={router}
>
...
</AppProvider>
Or by utilizing ReactRouterAppProvider
for seamless integration with react router:
export default function App() {
return (
<ReactRouterAppProvider navigation={NAVIGATION} branding={BRANDING}>
<Outlet />
</ReactRouterAppProvider>
);
}
...
export default function Layout() {
return (
<DashboardLayout>
<PageContainer>
<Outlet />
</PageContainer>
</DashboardLayout>
);
}
...
const router = createBrowserRouter([
{
Component: App, // root layout route
children: [
{
path: "/",
Component: Layout,
children: [
{
path: "diet",
children: [
{
path: "",
Component: DietPage,
},
{
path: "meals",
Component: MealsPage,
},
{
path: "calendar",
Component: CalendarPage,
},
],
},
{
path: "exercise",
Component: ExercisePage,
},
],
},
],
},
]);
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>
);
Here's codesandbox with working example:
https://codesandbox.io/p/sandbox/stupefied-lamport-d4stcf
You are just using the useDemoRouter
from the examples in the Docs. This is only used in the internal demos. As stated in the docs, the Toolpad Core doesn't handle routing itself. You need to implement your own routing solution.
From the Docs:
Toolpad Core doesn't handle routing itself. Instead, it's designed to integrate seamlessly with your existing routing solution, whether you're using:
- Next.js App Router
- Next.js Pages Router
- React Router
- Or any other routing library which implements the same interface
You can pass the router implementation to the AppProvider component using the router prop.
So you can implement one of the above routers. Here is an example using the React Router:
Use ReactRouterAppProvider
instead of AppProvider
.
import * as React from 'react';
import DashboardIcon from '@mui/icons-material/Dashboard';
import ShoppingCartIcon from '@mui/icons-material/ShoppingCart';
import { Outlet } from 'react-router';
import { ReactRouterAppProvider } from '@toolpad/core/react-router';
import type { Navigation } from '@toolpad/core/AppProvider';
const NAVIGATION: Navigation = [
{
kind: 'header',
title: 'Main items',
},
{
title: 'Dashboard',
icon: <DashboardIcon />,
},
{
segment: 'diet',
title: 'Diet',
icon: <ShoppingCartIcon />,
},
];
const BRANDING = {
title: 'My Toolpad Core App',
};
export default function App() {
return (
<ReactRouterAppProvider navigation={NAVIGATION} branding={BRANDING}>
<Outlet />
</ReactRouterAppProvider>
);
}
With this configuration you can use the React Router's useNavigate
hook.
Here is the CodeSandbox demo. I've used the same content for the Diet page. You can see that it is redirecting to the Meals page.
本文标签:
版权声明:本文标题:reactjs - How do I navigate to another page using the value of a Material UI Select component inside a Dashboard Layout? - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738591595a2101542.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
useDemoRouter
is only meant for the internal demos. You need to implement your own router. Checkout my answer below. – Raghavendra N Commented Jan 25 at 5:07