【问题标题】:MaterialUI 5 : TypeError: Cannot read properties of undefined (reading 'create')MaterialUI 5:TypeError:无法读取未定义的属性(读取“创建”)
【发布时间】:2021-11-19 00:42:42
【问题描述】:

创建 ThemeSetting.tsx 上下文后,我无法使用 <Button><Button> 以及所有使用 theme 的 materialUI 的东西 ReactJS , 打字稿

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({

和 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>;
};

【问题讨论】:

  • 您应该从@mui/material/styles 导入createTheme 和ThemeProvider。 @mui/system 中的版本是一个较低级别的部分,它不会针对 @mui/material 中的默认 Material Design 主题做任何特定的事情。

标签: reactjs typescript material-ui material-design


【解决方案1】:

我自己也遇到了。您可以从@mui/material/styles 或@mui/system 导入createTheme,但它们可以使用slightly different things:

您可以使用来自@mui/system 包的实用程序,或者如果您正在使用@mui/material,您可以从@mui/material/styles 导入它。不同之处在于使用的默认 theme(如果 React 上下文中没有可用的主题)。

来自@mui/material/styles 的那个足够聪明,可以填补默认 MUI 主题中活动主题中的空白。因此,如果您使用@mui/material,请使用@mui/material/styles。

【讨论】:

  • 这拯救了我的一天...谢谢
  • 我用过:import { createTheme } from '@mui/system'; 当我添加到我的代码内部 ThemeProvider 时,我的 组件在单击打开选项时崩溃了。以下导入:import { createTheme } from '@mui/material/styles'; 修复了我的主题中缺少的空白属性。我从来没有想过这会是问题所在,
猜你喜欢
  • 2020-10-15
  • 2020-05-28
  • 2018-10-29
  • 1970-01-01
  • 2021-11-26
  • 2021-11-24
  • 2021-12-20
  • 2021-11-27
  • 2022-01-21
相关资源
最近更新 更多