question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
eric-the-snakecommented, Oct 11, 2019

Just ran into the same problem.

In my case, changing import { withStyles } from '@material-ui/styles'; to import { withStyles } from '@material-ui/core/styles'; solved the issue. I guess the first package does not come with any Provider.

2reactions
7200rpmcommented, Jun 1, 2020

Just ran into the same problem.

In my case, changing import { withStyles } from '@material-ui/styles'; to import { withStyles } from '@material-ui/core/styles'; solved the issue. I guess the first package does not come with any Provider.

Unfortunately, this does not solve it for me. I can access theme when using makeStyles, but not withStyles. I am importing import { withStyles, makeStyles, useTheme } from '@material-ui/core/styles';

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found