【问题标题】:Creating a defaultProps with createTheme in MUI v5 not working在 MUI v5 中使用 createTheme 创建 defaultProps 不起作用
【发布时间】:2021-11-30 04:49:33
【问题描述】:

我是第一次使用 Material-UI v5,我想用createTheme 创建一个自定义主题来设置按钮组件的样式。根据documentation

主题的components 键允许您自定义组件而无需将其包装在另一个组件中。您可以更改样式、默认道具等。

他们提供了一个代码示例:

const theme = createTheme({
  components: {
    // Name of the component
    MuiButtonBase: {
      defaultProps: {
        // The props to change the default for.
        disableRipple: true, // No more ripple!
      },
    },
  },
});

但是当我尝试实现它时,似乎没有createTheme 的组件键。我收到以下错误:

Argument of type '{ components: any; }' is not assignable to parameter of type 'ThemeOptions'.
  Object literal may only specify known properties, and 'components' does not exist in type 'ThemeOptions'

这里可能有什么问题?

【问题讨论】:

  • 您使用的是 MUI v4 还是 v5?
  • 如果你使用 typescript,你可能需要定义你的主题的属性类型:mui.com/customization/theming
  • 我正在使用 v5 @NearHuscarl
  • 您的代码在 v5 上运行良好,您可以准备一个代码框吗?
  • 我正在使用打字稿,但我认为只有在创建自定义变量时才需要@BrunoFarias

标签: reactjs material-ui


【解决方案1】:

您使用的是 MUI v5,但是您从 v4 中导入了 createTheme,它在 createTheme 中没有 components 属性,因此将导入路径更改为:

import { createTheme } from "@mui/material/styles";

发件人:

import { createTheme } from "@material-ui/core";

【讨论】:

  • 非常感谢!
猜你喜欢
  • 2021-12-11
  • 2021-12-03
  • 2021-12-22
  • 2022-11-10
  • 2022-01-12
  • 2022-01-07
  • 2022-08-19
  • 2021-12-19
  • 2022-01-16
相关资源
最近更新 更多