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?
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.
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.
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.
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.
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).
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With