【发布时间】:2022-01-19 16:22:24
【问题描述】:
您能否解释一下我如何使我的路线与 MUI 选项卡一起使用? 由于我不会像这样编写代码并且不经常使用 MUI,因此我不明白如何使其工作。请问有什么想法吗?
- 我删除了导入以专注于主要问题并使代码更短。
这是我的 NavBar.js 组件:
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>
);
};
还有我的 AppRouter.js 组件
const AppRouter = () => {
return (
<ThemeProvider>
<Router>
<NavBar />
<Switch>
<Route exact path="/" component={Home} />
</Switch>
</Router>
</ThemeProvider>
);
};
【问题讨论】:
-
你试过什么?你只渲染一个带有
Home组件的路由。你想链接到什么?您可能已经为您的示例剥离了太多内容。 -
在我的 Switch 中,我添加了这样的新路由:
<Route exact path="/favorites" component={Favorites} />但是当我点击收藏选项卡时如何切换呢?我习惯使用 react-router 的 Link 来切换页面。
标签: javascript reactjs react-router material-ui