Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is makeStyles in material-ui? [duplicate]

I just started learning React material-ui and I found this makeStyles function, and they said it returns a Hook.

I remember from React hooks that a custom hook is made by wrapping a built-in hook. I tried looking inside makeStyles, and it has some interoperability and some css-in-javascript pattern.

I am really fed up with rules coming over and over again. For now can someone please tell me what is this makeStyle function and perhaps suggest a better approach to reading about material-ui.

Thank you good people of Stack Overflow.

like image 391
Parsuram Kailasa Avatar asked Mar 12 '20 08:03

Parsuram Kailasa


People also ask

What is the use of makeStyles in material UI?

makeStyles is a function from Material-UI that allows us to create CSS classes and rules using JavaScript objects. The makeStyles function returns a React hook that we can use in a functional component to access the styles and classes. Then, we can apply these styles to any element in our component.

What is the difference between makeStyles and withStyles?

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 .

What can I use instead of makeStyles in material UI?

you can use ThemeProvider to do that.. all you've to do is create a default theme with createTheme() then use that in ThemeProvider to pass it to all components in tree below ThemeProvider.. just use ThemeProvider to wrap your root component and that will provide default theme to each makeStyles that are currently in ...

Is makeStyles deprecated MUI?

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).


1 Answers

If you are familiar with the older version of Material-UI, you might have used withStyles, to use your custom styles in MUI components.

withStyles is just a HOC(Higher Order Component), used as a wrapper, to assign the classes prop to your component. You can furthur use the classes object to assign specific classes to your DOM or MUI elements in your component.

makeStyles is just a successor, which returns a hook(to access the custom classes). Now this is only a modern-react way of doing things in order to avoid HOCs.

MUI v3.9.3

import { withStyles } from '@material-ui/core/styles';

const styles = {
  root: {
    backgroundColor: 'red'
  },
};

class MyComponent extends React.Component {
  render () {
    return <div className={this.props.classes.root} />;
  }
}

export default withStyles(styles)(MyComponent);

MUI v4.9.5

import React from 'react';
import { makeStyles } from '@material-ui/core/styles';

const useStyles = makeStyles({
  root: {
    backgroundColor: 'red'
  },
});

export default function MyComponent(props) {
  const classes = useStyles(props);
  return <div className={classes.root} />;
}
like image 95
Barun Patro Avatar answered Oct 17 '22 13:10

Barun Patro