【发布时间】:2021-11-26 04:08:08
【问题描述】:
我在我的 react 应用程序中使用 Material Ui 选项卡进行路由。我可以设置默认 url(这样 '/' url 将重定向到 '/firsttab' 路由,但即使路由正确,这也不会使 'First' 选项卡显示为活动状态。我该如何做到这一点? 谢谢!
const routes = ["/firsttab", "/secondtab"];
function MainNavigation() {
return (
<div >
<IonToolbar >
<BrowserRouter >
<Route
path="/"
render={(history) => (
<AppBar>
<Tabs
className='mat-tab-nav-bar'
TabIndicatorProps={{style: {background:'primary'}}}
indicatorColor="primary"
color="primary"
variant="scrollable"
scrollButtons="auto"
aria-label="scrollable auto tabs"
value={history.location.pathname !== "/" ? history.location.pathname : false}
>
<Tab className="mat-tab"
label="First"
value={routes[1]}
component={Link}
to={routes[1]}
></Tab>
<Tab className="mat-tab "
label="Second"
value={routes[0]}
component={Link}
to={routes[0]}
></Tab>
</Tabs>
</AppBar>
)}
></Route>
<Switch >
<Route path="/scutes" component={Second}></Route>
<Route path="/gateways" component={First}></Route>
<Redirect exact from="/" to="/firsttab" /> <- this is the initial route redirect
</Switch>
</BrowserRouter>
</IonToolbar>
</div>
);
}
export default MainNavigation;
【问题讨论】:
标签: reactjs routes react-hooks material-ui