Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use react router Link with CardActionArea?

Can I use react router Link with CardActionArea?

Here is my code where my css is broken because How can I use Link component

import { BrowserRouter as Router, Route, Link } from "react-router-dom";
    <Link to="/Blog">
      <CardActionArea component="div" disableRipple>
        <CardMedia
          className={classes.media}
          image="images/marmik.jpg"
          title="Marmik Desai"
        />
        <CardContent>
          <Typography gutterBottom variant="h5" component="h2">
            Marmik Desai
          </Typography>
          <Typography variant="body2" color="textSecondary" component="p">
            I am front end developer.
          </Typography>
        </CardContent>
      </CardActionArea>
    </Link>

If i use below href="" in cardactionarea it will refresh page. I don't want to refresh page.

    <CardActionArea href="/Blog" disableRipple>

FOUND SOLUTION If I use Link as CardActionArea I need to overwrite Link css. I have found below solution.

import { BrowserRouter as Router, Route, Link } from "react-router-dom";
<CardActionArea component={Link} to="/Blog"> 
like image 588
Marmik Desai Avatar asked May 21 '19 06:05

Marmik Desai


2 Answers

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


<CardActionArea component={RouterLink} to={'/Blog'}>
    // ...
</CardActionArea >


like image 123
CodingByJerez Avatar answered Sep 17 '22 13:09

CodingByJerez


In CardActionArea set component prop to Link and to to your desired URL. This way you can use browser's back and forward button to navigate in your React application.

For example:

import { Link } from "react-router-dom";
import {
  Card,
  CardActionArea,
  CardContent,
  Typography,
} from "@mui/material";

<Card>
  <CardActionArea
    component={Link}
    to="YOUR-URL"
  >
    <CardContent>
      <Typography variant="h5">
        Card Title
      </Typography>
      <Typography variant="body2" color="text.secondary">
        Card Description
      </Typography>
    </CardContent>
  </CardActionArea>
</Card>
like image 33
smrachi Avatar answered Sep 16 '22 13:09

smrachi