Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the benefit of using withStyles over makeStyles?

Are there different use cases for each? When should one use withStyles over makeStyles?

like image 966
Akira Kotsugai Avatar asked Aug 28 '19 17:08

Akira Kotsugai


People also ask

What can I use instead of makeStyles?

You can use @mui/styles/makeStyles instead. @mui/styles is the legacy styling solution for MUI. It is deprecated in v5.

What is withStyles in React?

withStyles([ stylesThunk [, options ] ]) This is a higher-order function that returns a higher-order component used to wrap React components to add styles using the theme. We use this to make themed styles easier to work with.

What is the use of makeStyles?

makeStyles is a function that allows you to use JavaScript to styles your components. makeStyles uses JSS at its core, this essentially translates JavaScript to CSS. What makes this a great option is that it runs when the page is requested on the server side.

Is makeStyles deprecated?

It uses a syntax called CSS-in-JS (or JSS) which is just a JavaScript-friendly way of writing styles that compile into actual browser-readable CSS. Though deprecated, you can still use makeStyles() in MUI 5, but we expect it (to be removed in v6, but hence my confusion between past and present tense).


2 Answers

UPDATE This question/answer is geared towards v4 of Material-UI. I have added some v5 info/links at the end.


The Hook API (makeStyles/useStyles) can only be used with function components.

The Higher-order component API (withStyles) can be used with either class components or function components.

They both provide the same functionality and there is no difference in the styles parameter for withStyles and makeStyles.

If you are using it with a function component, then I would recommend using the Hook API (makeStyles). withStyles has a little bit of extra overhead compared to makeStyles (and internally delegates to makeStyles).

If you are customizing the styles of a Material-UI component, using withStyles is preferable to wrapping it with your own component solely for the purpose of calling makeStyles/useStyles since then you would just be re-implementing withStyles.

So wrapping a Material-UI component might look like the following example (from How to style Material-UI's tooltip?):

const BlueOnGreenTooltip = withStyles({
  tooltip: {
    color: "lightblue",
    backgroundColor: "green"
  }
})(Tooltip);

Edit Tooltip customization


For v5 of Material-UI, styled replaces withStyles and makeStyles. How to style Material-UI's tooltip? contains v5 examples. I also have further discussion of v5 styling options in Using conditional styles in Material-UI with styled vs JSS.

like image 85
Ryan Cogswell Avatar answered Oct 19 '22 11:10

Ryan Cogswell


When should one use withStyles over makeStyles?

Probably never but here are some use (narrow) cases:

  • you are running an react version that doesn't support hooks
  • you are writing an material-ui library and want to support older mui versions (withStyles is older than makeStyles)
  • you are running an older mui version
  • you want to style an class based component, for instance if you are interested in the componentDidCatch lifecycle method.
like image 12
Dawid Avatar answered Oct 19 '22 10:10

Dawid