我正在创建选项卡,并在第一个选项卡中添加路由。但是,单击链接后未显示所需的页面。尽管刷新页面后,相应的页面将显示在屏幕上。任何人都可以帮助我解决这个问题,因为我为此使用了实质性的用户界面
function TabPanel(props) {
const { children, value, index, ...other } = props;
return (
<Typography
component="div"
role="tabpanel"
hidden={value !== index}
id={`simple-tabpanel-${index}`}
aria-labelledby={`simple-tab-${index}`}
{...other}
>
<Box p={3}>{children}</Box>
</Typography>
);
}
TabPanel.propTypes = {
children: PropTypes.node,
index: PropTypes.any.isRequired,
value: PropTypes.any.isRequired,
};
function a11yProps(index) {
return {
id: `simple-tab-${index}`,
'aria-controls': `simple-tabpanel-${index}`,
};
}
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
backgroundColor: theme.palette.background.paper,
},
}));
export default function SimpleTabs() {
const classes = useStyles();
const [value, setValue] = React.useState(0);
const handleChange = (event, newValue) => {
setValue(newValue);
};
return (
<div className={classes.root}>
<AppBar position="static">
<Tabs value={value} onChange={handleChange} aria-label="simple tabs example">
<Tab label="Item One" {...a11yProps(0)} />
<Tab label="Item Two" {...a11yProps(1)} />
<Tab label="Item Three" {...a11yProps(2)} />
</Tabs>
</AppBar>
<TabPanel value={value} index={0}>
<Router>
<ul className="navbar-nav mr-auto" >
<li><Link to={'/Text'} className="nav-link"><i class="fas fa-text-height"></i> Text </Link></li>
</ul>
<div>
</div>
</Router>
</TabPanel>
<TabPanel value={value} index={1}>
Item Two
</TabPanel>
<TabPanel value={value} index={2}>
Item Three
</TabPanel>
<Router>
<Switch>
<Route exact path='/Text' component={Text} />
</Switch>
</Router>
</div>
);
}
所以请告诉我应该在哪里更改或代码有什么问题。
答案 0 :(得分:1)
为什么要两次声明Router
组件?应该有only one Router
个组件包装整个应用程序(不是在需要2+的情况下谈论特殊情况),这就是您的Link
和Route
不同步的原因。尝试这样的事情:
return (
<Router>
<div className={classes.root}>
<AppBar position="static">
<Tabs value={value} onChange={handleChange} aria-label="simple tabs example">
<Tab label="Item One" {...a11yProps(0)} />
<Tab label="Item Two" {...a11yProps(1)} />
<Tab label="Item Three" {...a11yProps(2)} />
</Tabs>
</AppBar>
<TabPanel value={value} index={0}>
<ul className="navbar-nav mr-auto" >
<li><Link to={'/Text'} className="nav-link"><i class="fas fa-text-height"></i> Text </Link></li>
</ul>
<div>
</div>
</TabPanel>
<TabPanel value={value} index={1}>
Item Two
</TabPanel>
<TabPanel value={value} index={2}>
Item Three
</TabPanel>
<Switch>
<Route exact path='/Text' component={Text} />
</Switch>
</div>
</Router>
);