Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Material UI react Button act as a react-router-dom Link?

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> 
like image 926
Diogo Capela Avatar asked Aug 01 '18 21:08

Diogo Capela


People also ask

How do you use a button as a link react?

To use a button as a link in React, wrap the button in an <a> tag or a Link component if using react router. The button will get rendered instead of the link and clicking on it will cause the browser to navigate to the specified page. Copied!

How do I add a link to a material UI button?

To make a React Material UI Raised Button link to an external URL, we can add a Button that has the variant prop set to contained . Then we set the href prop of the button to the URL we want to go to. to add a Button with the variant prop set to 'contained' to add a raised button.


1 Answers

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/.

like image 193
Diogo Capela Avatar answered Sep 22 '22 16:09

Diogo Capela