makeStyles: Cannot read property 'primary' of undefined
See original GitHub issueI 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.0react:^17.0.2typescript:^4.1.2node:v14.17.1npm: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:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top 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 >
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
@dummy26 I’ve updated the sandbox to use the latest versions for both
@mui/materialand@mui/stylesand 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/corewith@mui/stylesand because of the rename of the packages, they depend on different common context@material-ui/private-themingand@mui/private-theming.I hope the explanation helps.
@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! 🎉