makeStyles: Cannot read property 'primary' of undefined

See original GitHub issue

I using MUI next version and cannot make style.

  • @material-ui/core: ^5.0.0-beta.5
  • @mui/styles: ^5.0.0-rc.0
  • @emotion/react: ^11.4.1
  • @emotion/styled: ^11.3.0
  • react: ^17.0.2
  • typescript: ^4.1.2
  • node: v14.17.1
  • npm: 7.18.1

And my code make styles

src/assets/css/variables.tsx

import { Theme } from "@material-ui/core";
import { makeStyles } from "@mui/styles";

const variableStyle = makeStyles((theme: Theme) => ({

  '@global': {

    ':root': {
      '--primary': theme.palette.primary.main,
      '--secondary': theme.palette.secondary.main,
      '--danger': theme.palette.error.main,
      '--success': theme.palette.success.main,
      '--warning': theme.palette.warning.main,
      '--gray': '#eeeff5',
      '--yellow': '#f8ad08',
      '--pink': '#5b6bff',
      '--black': '#33375d',
      '--fontsize': '14px',
      '--sidebar-width': '288px',
      '--sidebar-compact-width': '70px'
    }

  }

}));

export default variableStyle;

src/views/components/includes/GlobalCss.tsx

import React from 'react';
import variableStyle from 'assets/css/variables';

const GlobalCss: React.FC = () => {

  variableStyle();

  return null;

}

export default React.memo(GlobalCss);

Error occured when I start project:

TypeError: Cannot read property 'primary' of undefined
/src/assets/css/variables.tsx:9

    6 | '@global': {
    7 | 
    8 |    ':root': {
>   9 |      '--primary': theme.palette.primary.main,
   10 | ^    '--secondary': theme.palette.secondary.main,
   11 |      '--danger': theme.palette.error.main,
   12 |      '--success': theme.palette.success.main,

What should I do to be able to use palette property of the theme variable inside makeStyles function

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
mnajdovacommented, Sep 3, 2021

@dummy26 I’ve updated the sandbox to use the latest versions for both @mui/material and @mui/styles and it works - https://codesandbox.io/s/theme-is-empty-object-forked-td5s0?file=/src/Intro.js

@nmchaves on your sandbox is because of a mismatch of dependencies I believe. You are using @material-ui/core with @mui/styles and because of the rename of the packages, they depend on different common context @material-ui/private-theming and @mui/private-theming.

I hope the explanation helps.

1reaction
nmchavescommented, Sep 3, 2021

@mnajdova It looks like I got “mentioned” instead of @ngvcanh by accident. Similar usernames 😄. But I’m glad you mentioned me since I hadn’t seen the v4 => v5 upgrade guide before. I’m excited to try out the v5 beta btw! 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

MUI - makeStyles - Cannot read properties of undefined
Since you're using v5, change your ThemeProvider , createTheme and makeStyles import path from: import { ThemeProvider, createTheme ...
Read more >
[styles][makeStyles] Uncaught TypeError: Cannot read ...
A Uncaught TypeError: Cannot read property 'refs' of undefined appears when the makeStyles detach function is called and sheetManager object ...
Read more >
Troubleshooting - Material UI - MUI
TypeError : Cannot read properties of undefined (reading 'pxToRem') This error results from trying to access an empty theme. Make sure that you...
Read more >
TypeError: Cannot read property 'attach' of undefined - Reddit
Whenever I add the Box component inside the Paper, I get error TypeError: Cannot read property 'attach' of undefined Here is my code....
Read more >
MUI - makeStyles - Cannot read properties of undefined-Reactjs
Coding example for the question MUI - makeStyles - Cannot read properties of undefined-Reactjs.
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