【发布时间】:2020-07-26 17:25:06
【问题描述】:
我使用 react 路由器实现了 material-ui 选项卡。当我单击一个选项卡时,选项卡指示器(选项卡下方的一条蓝线)按预期工作,它会移动到按下哪个选项卡。问题在于,当我重新加载页面时,选项卡指示器会返回到第一个选项卡项。
我想这可能是因为选项卡的初始值为零,所以当页面重新加载时,该值又变为零了。
import React, { useState } from 'react';
import Tabs from '@material-ui/core/Tabs';
import Tab from '@material-ui/core/Tab';
import SignIn from '../signIn/SignIn'
import SignUp from '../signUp/SignUp';
import About from '../about/About';
import NavbarStyles from './NavbarStyles';
import { Link, Switch, Route } from 'react-router-dom';
import { Paper } from '@material-ui/core';
/**
* This component handles the routing and navigating the user to different sections
*/
export default function Navbar() {
const classes = NavbarStyles();
const [value, setValue] = useState(0);
const handleChange = (event, newValue) => {
setValue(newValue);
};
return (
<div className={classes.root}>
<Paper>
<Tabs
value={value}
onChange={handleChange}
variant="scrollable"
scrollButtons="on"
indicatorColor="primary"
textColor="primary"
aria-label="scrollable force tabs example"
>
<Tab label="Sign In" to="/signin" component={Link} />
<Tab label="Sign Up" to="/signup" component={Link} />
<Tab label="About" to="/about" component={Link} />
</Tabs>
</Paper>
<Switch>
<Route component={SignIn} path="/signin" />
<Route component={SignUp} path="/signup" />
<Route component={About} path="/about" />
</Switch>
</div>
);
}
import { makeStyles } from '@material-ui/core/styles';
// This component contains the styles that is being used by its intented component
export default function NavbarStyles() {
const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
width: '100%',
backgroundColor: theme.palette.background.paper,
},
}));
return useStyles();
}
标签: reactjs tabs material-ui react-router-dom