I am working on a React project created with create-react-app that uses material UI and I am getting these errors:
as you can see it is something internal to material UI at the moment I set a custom theme:
import { ThemeProvider } from "@mui/material/styles";
import { CssBaseline } from "@mui/material";
import { CacheProvider } from "@emotion/react";
import { createEmotionCache } from "./utils/create-emotion-cache";
import { theme } from "./theme";
import SiteRoutes from "./routes";
import { createTheme } from "@mui/material";
export const theme = createTheme({...})
const clientSideEmotionCache = createEmotionCache();
function App(props) {
const { emotionCache = clientSideEmotionCache } = props;
return (
<>
<ThemeProvider theme={theme}>
<CacheProvider value={emotionCache}>
<CssBaseline />
<SiteRoutes />
</CacheProvider>
</ThemeProvider>
</>
);
}
export default App;
I found some solutions but none of them work except for one: delete react folder from node_modules. It seems that there are multiple imports for react as described here: https://reactjs.org/warnings/invalid-hook-call-warning.html#duplicate-react
my package.json looks like this:
{
"peerDependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"dependencies": {
"@emotion/cache": "^11.9.3",
"@emotion/react": "^11.9.3",
"@emotion/server": "^11.4.0",
"@emotion/styled": "^11.9.3",
"@mui/icons-material": "^5.8.4",
"@mui/lab": "^5.0.0-alpha.89",
"@mui/material": "^5.8.7",
"@mui/styles": "^5.8.7",
"chart.js": "^3.8.0",
"date-fns": "^2.28.0",
"formik": "^2.2.9",
"history": "^5.3.0",
"nprogress": "^0.2.0",
"prop-types": "^15.8.1",
"react-chartjs-2": "^4.2.0",
"react-helmet": "^6.1.0",
"react-perfect-scrollbar": "^1.5.8",
"react-router-dom": "^6.3.0",
"react-scripts": "^5.0.1",
"uuid": "^8.3.2",
"yup": "^0.32.11"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"devDependencies": {}
}
When executing npm ls react:
├─┬ @emotion/react@11.9.3
│ └── react@17.0.2 deduped
├─┬ @emotion/styled@11.9.3
│ └── react@17.0.2 deduped
├─┬ @mui/icons-material@5.8.4
│ └── react@17.0.2 deduped
├─┬ @mui/lab@5.0.0-alpha.89
│ ├─┬ @mui/base@5.0.0-alpha.88
│ │ └── react@17.0.2 deduped
│ ├─┬ @mui/system@5.8.7
│ │ ├─┬ @mui/styled-engine@5.8.7
│ │ │ └── react@17.0.2 deduped
│ │ └── react@17.0.2 deduped
│ ├─┬ @mui/utils@5.8.6
│ │ └── react@17.0.2 deduped
│ ├─┬ @mui/x-date-pickers@5.0.0-alpha.1
│ │ └── react@17.0.2 deduped
│ ├─┬ react-transition-group@4.4.2
│ │ └── react@17.0.2 deduped
│ ├── react@17.0.2 deduped
│ └─┬ rifm@0.12.1
│ └── react@17.0.2 deduped
├─┬ @mui/material@5.8.7
│ └── react@17.0.2 deduped
├─┬ @mui/styles@5.8.7
│ ├─┬ @mui/private-theming@5.8.6
│ │ └── react@17.0.2 deduped
│ └── react@17.0.2 deduped
├─┬ formik@2.2.9
│ └── react@17.0.2 deduped
├─┬ react-chartjs-2@4.2.0
│ └── react@17.0.2 deduped
├─┬ react-dom@17.0.2
│ └── react@17.0.2 deduped
├─┬ react-helmet@6.1.0
│ ├─┬ react-side-effect@2.1.2
│ │ └── react@17.0.2 deduped
│ └── react@17.0.2 deduped
├─┬ react-perfect-scrollbar@1.5.8
│ └── react@17.0.2 deduped
├─┬ react-router-dom@6.3.0
│ ├─┬ react-router@6.3.0
│ │ └── react@17.0.2 deduped
│ └── react@17.0.2 deduped
├─┬ react-scripts@5.0.1
│ └── react@17.0.2 deduped
└── react@17.0.2
Solutions tried:
- Delete react folder from node_modules: not ideal since this project is being worked with a team and will be deployed to aws.
- https://github.com/facebook/react/issues/13991#issuecomment-496383268: Did not work at all, import error, can only import from src folder.
- https://github.com/facebook/react/issues/13991#issuecomment-449597362: Did not work at all, added references to package.json,
yarn installyarn startand the same thing.
How to reproduce: https://github.com/goto3/React-Material-Admin-Dashboard I created a repo so that you can test this. Just execute npm i --force and npm start.
