I am new to React and Next.js, whilst upgrading from Node V16 to Node V18. One of the changes was to MUI from v4 to v5, currently using the packages
"@emotion/babel-plugin": "^11.11.0",
"@emotion/core": "^10.1.1",
"@emotion/react": "^11.11.4",
"@emotion/server": "^11.11.0",
"@emotion/styled": "^11.11.5",
"@mui/icons-material": "^5.15.13",
"@mui/lab": "^5.0.0-alpha.168",
"@mui/material": "^5.15.13".
"next": "14.1.3"
I ran all the codemods that were suggested on the official MUI documentation found here https://mui.com/material-ui/migration/v5-style-changes/. Unfortunately the solution did not find certain packages. So I Googled a bit and found this post: https://github.com/styled-components/styled-components/issues/2502. I copied his implementation then ran into this problem after some basic debugging. TypeError: Cannot read properties of undefined (reading ‘main’)
the _document.js code
import Document, { Html, Head, Main, NextScript } from 'next/document';
import { ServerStyleSheet } from 'styled-components';
import { renderToString } from 'react-dom/server';
import { extractCritical } from '@emotion/server';
export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const sheet = new ServerStyleSheet();
const originalRenderPage = ctx.renderPage;
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) =>
sheet.collectStyles(<App {...props} />),
});
const initialProps = await Document.getInitialProps(ctx);
// Extract critical CSS
const emotionStyles = extractCritical(<Main />);
const emotionCss = emotionStyles.css;
return {
...initialProps,
styles: (
<>
{initialProps.styles}
<style dangerouslySetInnerHTML={{ __html: emotionCss }} />
</>
),
};
} finally {
sheet.seal();
}
}
render() {
return (
<Html lang="en">
<Head>
<meta name="description" content="" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
</Head>
<body>
<noscript>You need JavaScript to use this web application</noscript>
<Main />
<NextScript />
</body>
</Html>
);
}
}
Loading.js code (the only place where it calls CircularProgress package
import * as React from 'react';
import CircularProgress from '@mui/material/CircularProgress';
import styled from '@emotion/styled'
import { createTheme, ThemeProvider } from '@mui/system';
const theme = createTheme();
const Container = styled.div`
height: 100vh;
width: 100vw;
display: flex;
justify-content: center;
align-items: center;
`;
function Loading() {
const [progress, setProgress] = React.useState(0);
React.useEffect(() => {
function tick() {
// reset when reaching 100%
setProgress((oldProgress) => (oldProgress >= 100 ? 0 : oldProgress + 1));
}
const timer = setInterval(tick, 20);
return () => {
clearInterval(timer);
};
}, []);
return (
<ThemeProvider theme={theme}>
<Container>
<CircularProgress variant="determinate" value={progress} />
</Container>
</ThemeProvider>
);
}
export default Loading;
and the error that is giving me:
TypeError: Cannot read properties of undefined (reading 'main')
at CircularProgressRoot.ownerState.ownerState (PATHnode_modules@muimaterialnodeCircularProgressCircularProgress.js:82:58)
at processStyleArg ([email protected]:66:67)
at [email protected]:172:25
at handleInterpolation (PATHnode_modules@emotionserializedistemotion-serialize.cjs.dev.js:149:24)
at Object.serializeStyles (PATHnode_modules@emotionserializedistemotion-serialize.cjs.dev.js:274:15)
at PATHnode_modules@emotionstyledbasedistemotion-styled-base.cjs.dev.js:167:34
at PATHnode_modules@emotionreactdistemotion-element-f93e57b0.cjs.dev.js:85:16
at renderWithHooks (PATHnode_modulesreact-domcjsreact-dom-server.browser.development.js:5658:16)
at renderIndeterminateComponent (PATHnode_modulesreact-domcjsreact-dom-server.browser.development.js:5731:15)
at renderElement (PATHnode_modulesreact-domcjsreact-dom-server.browser.development.js:5946:7)
at renderNodeDestructiveImpl (PATHnode_modulesreact-domcjsreact-dom-server.browser.development.js:6104:11)
at renderNodeDestructive (PATHnode_modulesreact-domcjsreact-dom-server.browser.development.js:6076:14)
at renderForwardRef (PATHnode_modulesreact-domcjsreact-dom-server.browser.development.js:5859:5)
at renderElement (PATHnode_modulesreact-domcjsreact-dom-server.browser.development.js:6005:11)
at renderNodeDestructiveImpl (PATHnode_modulesreact-domcjsreact-dom-server.browser.development.js:6104:11) {
page: '/'
}
I have tried reading Stack Overflow comments, asking Chatgpt, Perplexity AI, Youtube and CircularProgress documentation for guidance yet I still feel lost.