reactjs 具有React路由器域链接的SpeedDialAction

carvr3hs  于 2022-11-29  发布在  React
关注(0)|答案(1)|浏览(153)

我的SpeedDialAction如下所示:

<SpeedDialAction key="Add" icon={<AddIcon />} tooltipTitle="Add" />

我想在点击操作时导航到/add。有没有方法可以在不使用onClick和手动将/add推到历史记录的情况下实现这一点?
例如,一个按钮允许这样的操作:

<Button
  component={Link}
  to={`ticket/${ticket.id}`}
  variant="contained"
  color="primary"
>
  Details
</Button>
iswrvxsc

iswrvxsc1#

您可以使用<Link />环绕图标。
根据他们的示例,您可以如下 Package :

const withLink = (to, children) => <Link to={to}>{children}</Link>;

const actions = [
  { icon: withLink("/about", <LiveHelpIcon />), name: "About" },
  { icon: withLink("/users", <GroupIcon />), name: "Users" }
];

(You显然也可以以“正常”方式进行)

{ icon: <Link to="/about"><LiveHelpIcon /></Link>), name: "About" },

然后,用<Router> Package <SpeedDial />,并在其后面添加<Switch>,如下所示:

<Router>
  <SpeedDial
    ariaLabel="SpeedDial example"
    className={classes.speedDial}
    icon={<SpeedDialIcon />}
    onClose={handleClose}
    onOpen={handleOpen}
    open={open}
  >
    {actions.map(action => (
      <SpeedDialAction
        key={action.name}
        icon={action.icon}
        tooltipTitle={action.name}
        onClick={handleClose}
      />
    ))}
  </SpeedDial>
  <Switch>
    <Route path="/about">
      <div>About</div>
    </Route>
    <Route path="/users">
      <div>Users</div>
    </Route>
    <Route path="/">
      <div>Home</div>
    </Route>
  </Switch>
</Router>

工作示例:https://codesandbox.io/s/material-ui-speeddial-react-router-dom-bmmwi

相关问题