'makeStyles in Material UI not applying
I am trying to style my pahe using makeStyles from material ui with react js. it is working for me with some pages, but most of them are not working even if I am using the same styling elements like tha backgroundColor. in the code below, only pageTitle that is working.
import { Card, Paper, Typography } from '@mui/material'
import { makeStyles } from '@mui/styles';
import React from 'react'
const useStyles = makeStyles(theme => ({
root:{
backgroundColor:'#fdfd'
},
pageHeader:{
padding:theme.spacing(4),
display:'flex',
marginBottom:theme.spacing(3)
},
pageIcon:{
display: 'inline-block',
padding: theme.spacing(2),
color:'#AE431E'
},
pageTitle:{
paddingLeft: theme.spacing(4),
'& .MuiTypography-subtitle2':{
opacity: '0.5'
}
}
}))
export default function PageHeader(props) {
const classes = useStyles();
const {title,subtitle,icon} = props;
return (
<Paper className={classes.root} elevation={0} square >
<div classeName={classes.pageHeader}>
<Card className={classes.pageIcon}>
{icon}
</Card>
<div className={classes.pageTitle}>
<Typography
variant="h6"
component="div"
>{title}</Typography>
<Typography
variant="subtitle2"
component="div"
>{subtitle}</Typography>
</div>
</div>
</Paper>
)
}
Solution 1:[1]
You wrote classeName in the example, that's why it won't work.
Solution 2:[2]
A couple of months from the last answer on this thread but I would like to share what I did when I faced a similar issue:
- MUI5 components (such as Paper, Card, etc,) ignore the styles provided through
makeStyles
command. If you want to override a specific property you should use thestyled
command, wrapping the MUI5 component on it. For example, you can create a "hook"-kind function where you declare your specifications:
import { styled } from '@mui/material/styles';
import { Paper } from '@mui/material';
const usePageHeaderStyle = () => {
const styledPaper = styled(Paper)(({theme}) => ({
backgroundColor: theme.palette.background.default, //property to override
}));
return { styledPaper };
};
export default usePageHeaderStyle;
and then you can use it in your PageHeader
component:
export default function PageHeader(props) {
const {styledPaper: StyledPaper} = usePageHeaderStyle();
const {title,subtitle,icon} = props;
return (
<StyledPaper elevation={0} square >
...
</StyledPaper>
}
- Use
makeStyles
command to provide CSS-in-JS styles only for non MUI5 components (such asdiv
, etc,).
Hope this suggestion can help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Patrik Szabó |
Solution 2 | Nestor B. |