是否可以使用材料 ui 选项卡并仍然使用反应路由器?

chr*_*6tv 2 javascript jsx reactjs react-router material-ui

在材质 ui 中Tabs 组件中自定义了代码

  <AppBar position="static">
    <Tabs
      variant="fullWidth"
      value={value}
      onChange={handleChange}
      aria-label="nav tabs example"
    >
      <LinkTab label="Page One" href="/drafts" {...a11yProps(0)} />
      <LinkTab label="Page Two" href="/trash" {...a11yProps(1)} />

    </Tabs>
  </AppBar>
  <TabPanel value={value} index={0}>
    Page Onee
  </TabPanel>
  <TabPanel value={value} index={1}>
    Page Two
  </TabPanel>
Run Code Online (Sandbox Code Playgroud)

它基本上是两个选项卡,单击其中一个会显示文本“Page Onee”,单击另一个选项卡,则会显示文本“Page Two”。

但是,我希望能够使用反应路由器,以便我可以将一个组件带到每个选项卡并分配路由。在反应路由器中,它会是这样的(我知道反应路由器还有更多的事情要做,但在选项卡代码中就是这样):

<Link class="nav-link" to="/component1">Click here to see component1</Link>

<Link class="nav-link" to="/component2">Click here to see component2</Link>
Run Code Online (Sandbox Code Playgroud)

但是在我向您展示的第一段代码中(我从材料 ui 中定制的那个)我有这些<TabPanel>标签。

有没有一种简单的方法可以将反应路由器放入其中?

如果不可能包含反应路由器,我如何仍然能够使用该材质 ui 代码呈现我的组件?

Mic*_*ler 6

尝试将 material-ui 的 Tab 组件与toprops 和component={Link}props 一起使用。不要忘记import { Link } from 'react-router-dom';

<AppBar position="static">
 <Tabs
  variant="fullWidth"
  value={value}
  onChange={handleChange}
  aria-label="nav tabs example"
 >
   <Tab component={Link} label="Page One" to="/drafts" {...a11yProps(0)} />
   <Tab component={Link} label="Page Two" to="/trash" {...a11yProps(1)} />

 </Tabs>
</AppBar>
<TabPanel value={value} index={0}>
 Page Onee
</TabPanel>
<TabPanel value={value} index={1}>
 Page Two
</TabPanel>
Run Code Online (Sandbox Code Playgroud)

这对我来说可以