Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 342
Luis F. Avatar asked Nov 09 '17 16:11

Luis F.


People also ask

How do I add a link to a React list?

To add a link to a List with React Material UI, we can set the href prop of a ListItem component. We set the href prop of ListItem to a URL. And we set the component prop to 'a' to render the list item as a link. As a result, we should see a link rendered and it should go to https://www.google.com when we click on it.

What is material UI next?

js with Material-UI. Nextjs is a very popular server rendering library that so many people use for their development purposes. There are also so many UI libraries for server side rendering frameworks and Material UI is one of them. Material UI offers React components for faster and easier web development.


2 Answers

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

like image 143
Julian Botia Avatar answered Sep 24 '22 01:09

Julian Botia


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.

like image 35
Jules Dupont Avatar answered Sep 23 '22 01:09

Jules Dupont