Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material UI: Is it possible to put the makeStyle styles in a separate file? [duplicate]

I would be happy to be able to put the Hook API styles in a separate file for cleanliness purposes.

Do you know if it is possible and how it can be done? I didn't find anything aobut it in the official docs and everything I've tried has failed.

like image 729
Paulprg19 Avatar asked May 14 '20 07:05

Paulprg19


Video Answer


1 Answers

makeStyles returns you a useStyles hook that you call within your functional component. You can definitely move it out into a separate file and import the useStyles hook

styles.js

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

const useStyles = makeStyles({
  root: {
    backgroundColor: 'red',
    color: props => props.color,
  },
});

export { useStyles };

component.js

import React from 'react';
import {useStyles} from './styles.js'
export default function MyComponent(props) {
  const classes = useStyles(props);
  return <div className={classes.root} />;
}
like image 72
Shubham Khatri Avatar answered Dec 08 '22 03:12

Shubham Khatri