How to add a link to a List in material-ui 1.0?

丶灬走出姿态 提交于 2019-11-30 22:21:31

问题


The following messes with the onClick animation (the ListItem turns red):

<List>
  <a href="https://www.google.com">
    <ListItem button>
       <ListItemText primary="Google" />
     </ListItem>
   </a>
 </List>

While adding the link inside ListItem, only makes the transition work if ListItemText is clicked, which is not what I want. What is the correct way to add a link?


回答1:


The easiest way to accomplish this is to make the ListItem a link by using the component prop:

<List>
  <ListItem button component="a" href="https://www.google.com">
    <ListItemText primary="Google" />
  </ListItem>
</List>

That way, the ListItem will be an anchor tag linking to the desired place, but still receive the appropriate styling so that there won't be any visual changes.

The behavior of the component prop is documented here. Note that the href prop will be automatically passed to the anchor tag, as specified by the last line in the props documentation:

Any other properties supplied will be spread to the root element.




回答2:


to use with "react-router-dom"

import { Link } from "react-router-dom";
<ListItem button component={Link} to="/design">

the example is based in this section: docs



来源:https://stackoverflow.com/questions/47206639/how-to-add-a-link-to-a-list-in-material-ui-1-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!