37

After I created ThemeSetting.tsx context I cannot use <Button><Button> and all things that use theme of Material UI React.js, TypeScript:

error

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>;
};
1
  • 4
    You should be importing createTheme and ThemeProvider from @mui/material/styles. The version in @mui/system is a lower-level piece that does not do anything specific to the default Material Design theme in @mui/material. Commented Sep 26, 2021 at 18:36

1 Answer 1

84

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 default theme 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.

Sign up to request clarification or add additional context in comments.

1 Comment

I used: import { createTheme } from '@mui/system'; and when I add to my code inner ThemeProvider my component of <TexeField select /> crashed when click to open the options. The following import: import { createTheme } from '@mui/material/styles'; fixied that missing gaps properties in my theme. I would never have thought in that direction that this would be the problem,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.