Skip to content
Advertisement

How do I make a reusable component for the Cards and how can I use the new makeStyles of material-ui?

In the demo.js, I wanted to use the Card along with its CardHeader and put wordings inside the CardContent. Also, I will be using the CardComponent in other files as well. How can I make the CardComponent reusable?

Sample codes: https://codesandbox.io/s/basiccard-material-demo-forked-kkshx?file=/demo.js

Below are the codes for the demo.js:

JavaScript

Below are the codes for the CardComponent:

JavaScript

I have another question as well under this component. Coming from material-ui4, mui5 was kind of confusing as I can no longer use the makeStyles. I tried adding this in the codesandbx example, however, it will say that dependency not found:

JavaScript

Hence, I settled with const CardStyle. Any help on how can I implement the newer version of the makeStyles? Also, do I need to install other dependecies from material-ui to make it work? Below is the package.json file.

enter image description here

Also in: https://www.reddit.com/r/reactjs/comments/sgqhh3/how_do_i_make_a_reusable_component_for_the_cards/

Advertisement

Answer

You can make it reusable adding props to your child card component. You can add as many props you want to make different cards using the same pattern.

To give styles to the component in mui v5 you have to use styled. You have to import it from @mui/material/styles

Here is the whole code of the new card component

JavaScript

And here it is the whole codesandbox.

Edit BasicCard Material Demo (forked)

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement