score:88

Accepted answer

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.

score:0

import react, { forwardref } from "react";
// material-ui components
import listitem from "@material-ui/core/listitem";
import listitemicon from "@material-ui/core/listitemicon";
import listitemtext from "@material-ui/core/listitemtext";
// react-router
import { link as routerlink } from "react-router-dom";
const listitemlink = (props) => {
  const { icon, primary, to } = props;

  const renderlink = react.usememo(
    () =>
      forwardref((itemprops, ref) => (
        <routerlink to={to} ref={ref} {...itemprops} />
      )),
    [to]
  );

  return (
    <>
      <listitem button component={renderlink}>
        {icon ? <listitemicon>{icon}</listitemicon> : null}
        <listitemtext primary={primary} />
      </listitem>
    </>
  );
};

export default listitemlink;

it's actually available in the docs.

score:0

for the update to @mui/material (i believe 5.x.x)..

i don't feel this solution is ideal, and maybe someone can iterate on it, but i've made a simplified version of what the official docs now say to follow https://mui.com/guides/routing/#list (and then check the list example which is pretty convoluted:

you will want to do something like the following:

<list>
  <listitemlink to="/" text="home" />
</list>

where listitemlink is the following:

const listitemlink = ({to, text}) => {
  const renderlink = react.usememo(
    () =>
      react.forwardref(function link(itemprops, ref) {
        return <routerlink to={to} ref={ref} {...itemprops} role={undefined} />;
      }),
    [to],
  );

  return (
    <listitem button component={renderlink}>
      <listitemtext primary={text} />
    </listitem>
  )
}

you will also need to make sure that you:

import {
  route, link as routerlink, linkprops as routerlinkprops, routes
} from "react-router-dom";

(i have included routerlinkprops here for those using typescript, but the full example which uses typescript is in the recommended link: https://mui.com/guides/routing/#list)

score:1

for usage with next.js, this worked for me:

import link from "next/link";

<list>
    <link href="/myurl" passhref>
        <listitem button component="a">
            my link text
        </listitem>
    </link>
</list>

score:2

i have faced the same issue but maybe a new update in materialui due to this is not working, there has some tweak as import from import link from '@material-ui/core/link';

so it will works

 import link from '@material-ui/core/link';

 <list>
  <listitem button component={link} href="/dsda">
    <listitemicon>
      <dashboardicon />
    </listitemicon>
    <listitemtext primary="dashboard"/>
  </listitem>
 </list>

render link in material ui drawer

score:163

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


Related Query

More Query from same tag