Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use Material-UI Link with react-router-dom Link?

Given these two components:

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

Is there a way to get the style from Material-UI with the functionality of react-router-dom?

like image 570
Marcelo Velasquez Avatar asked Aug 02 '20 13:08

Marcelo Velasquez


1 Answers

You can use the component prop of Material-UI's Link to integrate with Link in react-router-dom. You can do the same thing with Material-UI's Button.

Here's an example showing both:

import React from "react";
import { Route } from "react-router";
import { BrowserRouter as Router, Link as RouterLink } from "react-router-dom";
import Link from "@material-ui/core/Link";
import Button from "@material-ui/core/Button";

export default function LinkRouter() {
  return (
    <Router>
      <div>
        <Link component={RouterLink} to="/">
          Link to Home
        </Link>
        <br />
        <Link component={RouterLink} to="/inner">
          Link to inner page
        </Link>
        <br />
        <Button
          variant="contained"
          color="primary"
          component={RouterLink}
          to="/inner"
        >
          Button to inner page
        </Button>
        <Route path="/" exact>
          <div>Here's Home</div>
        </Route>
        <Route path="/inner">
          <div>Here's the inner page</div>
        </Route>
      </div>
    </Router>
  );
}

Edit Use Link with react-router Link

Documentation: https://material-ui.com/guides/composition/#link

like image 90
Ryan Cogswell Avatar answered Nov 14 '22 09:11

Ryan Cogswell