'MaterialUI 5 : TypeError: Cannot read properties of undefined (reading 'create')
After I created ThemeSetting.tsx context I cannot use
<Button><Button>
and all things that use theme
of materialUI
ReactJS , Typescript
TypeError: Cannot read properties of undefined (reading 'create') push../node_modules/@mui/material/Button/Button.js.Object.ownerState.ownerState node_modules/@mui/material/Button/Button.js:67
64 | minWidth: 64,
65 | padding: '6px 16px',
66 | borderRadius: theme.shape.borderRadius,
> 67 | transition: theme.transitions.create(['background-color', 'box-shadow', 'border-color', 'color'], {
| ^ 68 | duration: theme.transitions.duration.short
69 | }),
70 | '&:hover': _extends({
and ThemeSetting.tsx
import { createTheme, ThemeProvider } from "@mui/system";
export const ThemeContextProvider: React.FC<{}> = ({ children }) => {
const theme = createTheme({
palette: {
navbar: blue[100],
tag: {
red: red[200],
pink: pink[200],
purple: purple[200],
blue: blue[200],
green: green[200],
},
},
typography: {
fontFamily: [
"NotoSans",
"NotoSansThai",
"Arial",
"Roboto",
"'Helvetica Neue'",
"sans-serif",
].join(","),
},
shape: {
borderRadius: 15,
},
});
return <ThemeProvider theme={theme}>{children}</ThemeProvider>;
};
Solution 1:[1]
Just ran into this myself. You can import createTheme
from @mui/material/styles
or @mui/system
, but they do slightly different things:
You can use the utility coming from the
@mui/system
package, or if you are using@mui/material
, you can import it from@mui/material/styles
. The difference is in the defaulttheme
that is used (if no theme is available in the React context).
The one from @mui/material/styles
is smart enough to fill in gaps in the active theme from the default MUI theme. So if you're using @mui/material
, then use @mui/material/styles
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | mrdecemberist |