Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use 'classes' vs 'className' with Material UI?

Tags:

I'm a little confused with the two properties. If I have,

const useStyles = makeStyles(() => ({   style: {     width: 600,     height: 400,   }, })); 

Then, I can do,

const classes = useStyles();  <SomeComponent className={classes.style} /> 

But I can also do,

const classes = useStyles();  <SomeComponent classes={classes} /> 

What is the difference between these two?

like image 449
Mike K Avatar asked Mar 04 '20 09:03

Mike K


People also ask

Why you should not use material UI?

Material-UI is a great library and has had my back in the past years, yet now I would strongly advise against using it if your application might present performance challenges. A simple form or a standard web page shouldn't be a problem, but still, keep that in mind.

How do I give className in material UI?

To customize a specific part of a component, you can use the class name provided by Material UI inside the sx prop. As an example, let's say you want to change the Slider component's thumb from a circle to a square. First, use your browser's dev tools to identify the class for the component slot you want to override.

Is material UI good for performance?

Material UI provides developers with material-based UI components. They help save some time on design and app development. But since Material UI is mostly a set of UI components, it doesn't offer such a great boost to the development speed as Bootstrap does.

How do I add multiple Classnames in material UI?

To add multiple classes in React Material UI using the classes props, we can use the classnames package. We call makeStyles with an object with the class names as the property names. And we set each property to an object with the CSS styles as the value. Next, we call useStyles to return the classes object.


2 Answers

This is a very confusing aspect of MUI, but once you get it - it's super easy.

Consider that YOU are writing a component, and style it using JSS:

const useStyles = makeStyles(theme => ({   root: {     margin: theme.spacing(1)   },   in: {     padding: 8   } }));  function MyComponent(){   const classes = useStyles();   return (     <Outside className={classes.root}>       <Inside className={classes.in} />     </Outside>   ) } 

Notice that the component is essentially a composition (or a hierarchy) of components - Outside and Inside in this minimal example, but MUI components often have more than two (styled) nested components.

Now you want to export this component as part of a library and allow developers to style all the components involved (both Outside and Inside). How would you do it?

What MUI does, is it allows you to provide a classes property (you'll see in the docs each component's rule names - root and in in our example) that will be merged into MUI's own rules, or stylesheet if you wish (in the MUI code this is done using the withStyles HOC).

For convenience, every component also accepts className property that is merged into the className of the root element (Outside in our case).

like image 152
Izhaki Avatar answered Sep 20 '22 15:09

Izhaki


className is always applied to the root element of the component whereas classes gives you deeper access to style child elements of the component via the object key of the style object. It's explained in the documentation here:

https://material-ui.com/customization/components/

like image 28
net.uk.sweet Avatar answered Sep 21 '22 15:09

net.uk.sweet