theme object is not accessable in withStyles (import { withStyles } from '@material-ui/styles')
See original GitHub issue- This is not a v0.x issue.
- I have searched the issues of this repository and believe that this is not a duplicate.
Expected Behavior 🤔
Want to use ‘theme’ object in styles like below,
const styles = theme => ({
root: {
background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
borderRadius: 3,
border: 0,
color: 'white',
height: theme.spacing.unit *7,
[theme.breakpoints.down('sm')]:{
height:theme.spacing.unit *10
},
padding: '0 30px',
boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)',
},
});
Current Behavior 😯
Whenever i import withStyles from @material-ui/styles, theme object is not accessable. I am getting error
TypeError: Cannot read property ‘unit’ of undefined
Examples 🌈
Please find my code below, if i import withStyles from @material-ui/core/styles, i am able to apply the theme as expected.
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
//import { withStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
import { withStyles } from '@material-ui/styles';
const styles = theme => ({
root: {
background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
borderRadius: 3,
border: 0,
color: 'white',
height: theme.spacing.unit *7,
[theme.breakpoints.down('sm')]:{
height:theme.spacing.unit *10
},
padding: '0 30px',
boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)',
},
});
function ClassNames(props) {
const { classes, children, className, ...other } = props;
console.log(classes)
return (
<Button className={classNames(classes.root, className)} {...other}>
{children || 'class names'}
</Button>
);
}
ClassNames.propTypes = {
children: PropTypes.node,
classes: PropTypes.object.isRequired,
className: PropTypes.string,
};
export default withStyles(styles)(ClassNames);
Context 🔦
I am trying to use withStyles from @material-ui/styles. Theme object is not accessable inside the styles block. whereas @material-ui/core/styles is working as expected.
Please help me to identify where i am missing the code here.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
theme object is not accessable in withStyles (import ... - GitHub
I am trying to use withStyles from @material-ui/styles. Theme object is not accessable inside the styles block. whereas @material-ui/core/styles ...
Read more >Material-UI withStyles doesn't apply any kind of styles
You should import withStyles from '@material-ui/core/styles' (instead of '@material-ui/styles') in order to have the default theme automatically ...
Read more >@mui/styles (LEGACY) - MUI System
It depends on JSS as a styling solution, which is not used in the @mui/material anymore, ... import { withStyles } from '@mui/styles';...
Read more >Advanced (LEGACY) - MUI System
The makeStyles (hook generator) and withStyles (HOC) APIs allow the creation of multiple style rules per style sheet. Each style rule has its...
Read more >Styles API - MUI System
Its only purpose is to defeat TypeScript's type widening when providing style rules to makeStyles / withStyles which are a function of the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Just ran into the same problem.
In my case, changing
import { withStyles } from '@material-ui/styles';
toimport { withStyles } from '@material-ui/core/styles';
solved the issue. I guess the first package does not come with anyProvider
.Unfortunately, this does not solve it for me. I can access theme when using
makeStyles
, but notwithStyles
. I am importingimport { withStyles, makeStyles, useTheme } from '@material-ui/core/styles';