Source:  Twitter logo

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>

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://next.material-ui.com/guides/routing/.

125 users liked answer #0dislike answer #0125
Diogo Capela profile pic
Diogo Capela

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>
17 users liked answer #1dislike answer #117
andromeda profile pic
andromeda

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>
)
11 users liked answer #2dislike answer #211
Peeyush Kumar profile pic
Peeyush Kumar

You 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>
      ...
    </>
)
}
0 users liked answer #3dislike answer #30
Isuru Bandara profile pic
Isuru Bandara

Copyright © 2022 QueryThreads

All content on Query Threads is licensed under the Creative Commons Attribution-ShareAlike 3.0 license (CC BY-SA 3.0).