72

How can I make a Material UI react Button component act as a Link component from react-router-dom without losing it's original style? Like changing the route on click.

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

<Button variant="contained" color="primary">
    About Page
</Button>

To something like this, but maintaining the original Button style:

import Button from '@material-ui/core/Button';
import { Link } from 'react-router-dom';

<Button variant="contained" color="primary">
    <Link to="/about">
        About Page
    </Link>
</Button>
Diogo Capela
  • 5,669
  • 5
  • 24
  • 35
  • Possible duplicate of [Material-ui adding Link component from react-router](https://stackoverflow.com/questions/37843495/material-ui-adding-link-component-from-react-router) – Tholle Aug 01 '18 at 21:46

4 Answers4

159

Okay, this is very easy, I don't know why it was not working with me:

Just do like this:

import Button from '@material-ui/core/Button';
import { Link } from 'react-router-dom';

<Button component={Link} to="/about" variant="contained" color="primary">
  About Page
</Button>

You can find more details at https://mui.com/material-ui/guides/routing/.

Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
Diogo Capela
  • 5,669
  • 5
  • 24
  • 35
31

MUI 5 has simplified this even further. Simply provide a MUI Button with a href attribute as follows:

import Button from '@mui/material/Button';


<Button href="/" variant="contained">
  Link
</Button>
andromeda
  • 4,433
  • 5
  • 32
  • 42
13

You need to wrap the <Button /> inside the <Link /> component.

import Button from '@material-ui/core/Button';
import { Link } from 'react-router-dom';

const ButtonWithLink = () => (
  <Link to="/about">
   <Button variant="contained" color="primary">
     About Page
   </Button>
  </Link>
)
Peeyush Kumar
  • 723
  • 6
  • 10
  • 2
    this way also works. just need to give styling to remove underline from `Link`. Thanks – Valay Aug 03 '19 at 16:52
  • This was extremely helpful for a problem I was having where I have a wrapper for `Link` and so the `component={Link}` method resulted in lost styling from Material-UI for some reason. This method restored the styling. – c_sagan Dec 16 '19 at 16:56
  • 4
    A Button inside of a Link is not valid HTML (see also https://stackoverflow.com/questions/6393827/can-i-nest-a-button-element-inside-an-a-using-html5) and even if parsable by browsers, its poor semantics and causes a11y problems. – filoxo Jul 12 '22 at 03:51
0

This way worked for me

import Button from '@material-ui/core/Button';
import { useHistory } from 'react-router-dom';

const YourComponentName = () =>{

    const history = useHistory();
    const handleRoutes = (path) =>{
       history.push(path)
    }
    
  return(
    <>
      ...
      <Button 
         variant="contained" 
         color="primary" 
         onClick={() => handleRoutes('/about')}>
         About Page
      </Button>
      ...
    </>
)
}
  • It will not behave the same as link, e.g.: you cannot click mouse wheel to open link in another tab. I would not recommend this approach as it may result in poor UX. – Mantas Giniūnas Apr 04 '23 at 13:29