mirror of
https://github.com/kou029w/_.git
synced 2025-02-07 09:38:46 +00:00
use useRouter
This commit is contained in:
parent
5b71359f13
commit
e25f4ac6cf
1 changed files with 14 additions and 19 deletions
|
@ -1,36 +1,31 @@
|
||||||
import React, { useState } from "react";
|
import React from "react";
|
||||||
import { withRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
import { AppBar, Tabs, Tab } from "@material-ui/core";
|
import { AppBar, Tabs, Tab } from "@material-ui/core";
|
||||||
import routes from "../routes";
|
import routes from "../routes";
|
||||||
|
|
||||||
const TopAppBar = withRouter(({ router }) => {
|
const TopAppBar = () => {
|
||||||
const [value, setValue] = useState(
|
const router = useRouter();
|
||||||
routes.findIndex(({ pathname }) => pathname === router.pathname)
|
const pathname = (
|
||||||
);
|
routes.find(({ pathname }) => pathname === router.pathname) || {
|
||||||
router.events.on("routeChangeStart", (url: string) => {
|
pathname: "/"
|
||||||
setValue(
|
}
|
||||||
routes.findIndex(
|
).pathname;
|
||||||
({ pathname }) => pathname === url.replace(/(?<=.)\/$/, "")
|
|
||||||
)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
return (
|
return (
|
||||||
<AppBar color="default">
|
<AppBar color="default">
|
||||||
<Tabs
|
<Tabs
|
||||||
value={value}
|
value={pathname}
|
||||||
onChange={(_, value) => {
|
onChange={(_, value) => {
|
||||||
router.push(routes[value].pathname);
|
router.push(value);
|
||||||
setValue(value);
|
|
||||||
}}
|
}}
|
||||||
variant="fullWidth"
|
variant="fullWidth"
|
||||||
indicatorColor="primary"
|
indicatorColor="primary"
|
||||||
textColor="primary"
|
textColor="primary"
|
||||||
>
|
>
|
||||||
{routes.map(({ icon }, i) => (
|
{routes.map(({ pathname, icon }) => (
|
||||||
<Tab key={i} icon={icon} />
|
<Tab key={pathname} value={pathname} icon={icon} />
|
||||||
))}
|
))}
|
||||||
</Tabs>
|
</Tabs>
|
||||||
</AppBar>
|
</AppBar>
|
||||||
);
|
);
|
||||||
});
|
};
|
||||||
export default TopAppBar;
|
export default TopAppBar;
|
||||||
|
|
Loading…
Add table
Reference in a new issue