admin管理员组文章数量:1279212
Can you please explain how I make my routes work with MUI tabs? Since I don't write my code like this and do not use MUI very often, I don't understand how to make it work. any ideas please?
- I removed the imports to focus on the main problem and make the code shorter.
This is my NavBar.js Component:
const NavBar = props => {
const [value, setValue] = useState(0);
const handleChange = (_e, newValue) => {
setValue(newValue);
};
return (
<AppBar position="static" color="transparent" style={{ position: "fixed", top: 0 }}>
<Tabs
value={value}
onChange={handleChange}
aria-label="Navigation"
indicatorColor="primary"
textColor="primary"
>
<Tab label="Home" index={0} />
<Tab label="Favorites" index={1} />
</Tabs>
</AppBar>
);
};
And my AppRouter.js Component
const AppRouter = () => {
return (
<ThemeProvider>
<Router>
<NavBar />
<Switch>
<Route exact path="/" ponent={Home} />
</Switch>
</Router>
</ThemeProvider>
);
};
Can you please explain how I make my routes work with MUI tabs? Since I don't write my code like this and do not use MUI very often, I don't understand how to make it work. any ideas please?
- I removed the imports to focus on the main problem and make the code shorter.
This is my NavBar.js Component:
const NavBar = props => {
const [value, setValue] = useState(0);
const handleChange = (_e, newValue) => {
setValue(newValue);
};
return (
<AppBar position="static" color="transparent" style={{ position: "fixed", top: 0 }}>
<Tabs
value={value}
onChange={handleChange}
aria-label="Navigation"
indicatorColor="primary"
textColor="primary"
>
<Tab label="Home" index={0} />
<Tab label="Favorites" index={1} />
</Tabs>
</AppBar>
);
};
And my AppRouter.js Component
const AppRouter = () => {
return (
<ThemeProvider>
<Router>
<NavBar />
<Switch>
<Route exact path="/" ponent={Home} />
</Switch>
</Router>
</ThemeProvider>
);
};
Share
Improve this question
edited Dec 17, 2021 at 0:52
zolastro
93713 silver badges30 bronze badges
asked Dec 16, 2021 at 15:12
ArthurArthur
1011 gold badge1 silver badge4 bronze badges
2
-
What have you tried? You only render one route with
Home
ponent on it. What are you trying to link to? You might've stripped out too much for your example. – Drew Reese Commented Dec 16, 2021 at 17:25 -
In my Switch I added new Route like this:
<Route exact path="/favorites" ponent={Favorites} />
But how I make it switch when I click on the Favorite Tab? I'm used to use Link from react-router to switch between pages. – Arthur Commented Dec 16, 2021 at 18:08
4 Answers
Reset to default 6Ok so I found the solution. In my AppRouter.js I got this:
import React from "react";
import { HashRouter as Router, Switch, Route } from "react-router-dom";
import { Home, Favorites } from "pages";
import { ThemeProvider } from "theme";
import NavBar from "ponents/NavBar";
const AppRouter = () => {
return (
<ThemeProvider>
<Router>
<NavBar />
<Switch>
<Route exact path="/favorites" ponent={Favorites} />
<Route exact path="/" ponent={Home} />
</Switch>
</Router>
</ThemeProvider>
);
};
export default AppRouter;
and in my NavBar.js I added this and it allows me to switch between them:
import React, { useState } from "react";
import { AppBar, Tabs, Tab } from "@material-ui/core";
import { Home, Favorites } from "pages";
import { Route, BrowserRouter, Switch, Link } from "react-router-dom";
const NavBar = props => {
const [value, setValue] = useState(0);
const handleChange = (_e, newValue) => {
setValue(newValue);
};
return (
<AppBar position="static" color="transparent" style={{ position: "fixed", top: 0 }}>
<Tabs
value={value}
onChange={handleChange}
aria-label="Navigation"
indicatorColor="primary"
textColor="primary"
>
<Tab label="Home" index={0} ponent={Link} to={"/"} />
<Tab label="Favorites" index={1} ponent={Link} to={"/favorites"} />
</Tabs>
</AppBar>
);
};
export default NavBar;
the problem I face now is that for some reason I lose my local storage and I change my route
Did you try to add in handleChange
hard coded routing, like history.push
based on value? Not sure is it what you want.
I found the best solution for this is to first create a layout to define the tabs and handle the routing logic:
export default function Layout({children}: {children: any}) {
const [tabValue, setTabValue] = useState('monitor');
const router = useRouter();
// Navigate to the new page
const handleTabChange = (event: React.SyntheticEvent, newTabValue: string) => {
switch(newTabValue) {
case 'about':
router.push('/about')
break
case 'monitor':
router.push('/')
break
}
};
// Update the tab to make it active
useEffect(() => {
switch(router.pathname) {
case '/':
setTabValue('monitor')
break
case '/about':
setTabValue('about')
break
}
}, [router.pathname])
And then after this define each of the pages you wish the tabs to display using the Layout eg:
import Layout from '@/ponents/Layout'
import Monitor from '@/pages/Monitor';
export default function Home() {
return (
<Layout>
<Monitor />
</Layout>
);
}
Here's my solution using "react-router-dom": "^6.28.0"
and "@mui/material": "^6.1.8"
.
I'm using the createBrowserRouter
method to define my routes:
sample routes:
.
.
{
path: 'invoices/*',
lazy: () => import('./pages/ListInvoices'),
children: [
{ path: '*', element: <Navigate replace to="sent" /> },
{ path: 'sent', element: <SentInvoices /> },
{ path: 'received', element: <ReceivedInvoices /> },
],
}
.
.
ListInvoices.tsx
import Tab from '@mui/material/Tab';
import Tabs from '@mui/material/Tabs';
import { Link, Outlet, useLocation } from 'react-router-dom';
Component.displayName = 'ListInvoicesPage';
export function Component() {
const { pathname } = useLocation();
const currentRoute = pathname.replace('/invoices/', '').split('/')[0];
return (
<>
<Tabs value={currentRoute}>
<Tab value="sent" label="Sent" ponent={Link} to={'../sent'} />
<Tab value="received" label="Received" ponent={Link} to={'../received'} />
</Tabs>
<Outlet />
</>
);
}
本文标签: javascriptHow to use react router with MUI tabsStack Overflow
版权声明:本文标题:javascript - How to use react router with MUI tabs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741222331a2361200.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论