简体   繁体   English

使用 Material-UI 图标进行导航

[英]Navigation with Material-UI Icons

How can I use a Material-UI Icon to do navigation using React router Dom?如何使用 Material-UI 图标使用 React 路由器 Dom 进行导航?

I tried this but this doesn't work:我试过这个,但这不起作用:

 <NavigateBeforeIcon path="/vehicles"></NavigateBeforeIcon>

In case of Buttons, I could do something like component={Link} and to="/vehicles" but I can't figure out how to navigate directly from the icon.如果是按钮,我可以执行类似component={Link}to="/vehicles"的操作,但我不知道如何直接从图标导航。

Did you try this way?你试过这种方法吗? Material-UI now supports Icon Button components, Material-UI 现在支持 Icon Button 组件,

<IconButton aria-label="vehicles" component={Link} to="/vehicles">
  <NavigateBeforeIcon />
</IconButton>

Here is a working example: https://codesandbox.io/s/material-demo-ifgg2这是一个工作示例: https://codesandbox.io/s/material-demo-ifgg2

I found it weird that I couldn't find your exact icon from the Icon pack.我发现我无法从图标包中找到您的确切图标,这很奇怪。 I replaced that icon with a working sample icon.我用一个工作示例图标替换了那个图标。 However, This could help you to identify and use as proof for my answer.但是,这可以帮助您识别并用作我回答的证据。

Refer to this: https://material-ui.com/components/buttons/#icon-buttons参考这个: https://material-ui.com/components/buttons/#icon-buttons

I hope this will help you!我希望这能帮到您!

You can use theuseHistory Hook:你可以使用useHistory钩子:

import { useHistory } from 'react-router-dom';从 'react-router-dom' 导入 { useHistory };

function Home() {
  const history = useHistory();

  return <NavigateBeforeIcon onClick={() => history.push('/profile')}
          ></NavigateBeforeIcon>
}

With it you can build a custom component.有了它,您可以构建自定义组件。

You can use react-router-dom's useHistory hook or from props to push a path whenever the icon is clicked.您可以使用 react-router-dom 的 useHistory 钩子或从道具在单击图标时推送路径。

const Sidepane = () => {

// Assuming that you've imported it from the react-router-dom package

const history = useHistory()

const onClick = () => history.push("/vehicles") 

return (
        <NavigateBeforeIcon onClick={onClick}>
           Go to vehicles 
        </NavigateBeforeIcon

        )

}


I hope this helps我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM