【问题标题】:React Material-UI: the key provided to the classes property is not implemented in undefinedReact Material-UI:提供给类属性的键未在未定义中实现
【发布时间】:2019-11-10 07:39:25
【问题描述】:

我正在尝试使用 makeStyles() 覆盖 Material-UI 样式,如 docs 中所述。但是当我在父组件的类道具中传递一个类名时,我收到了一个警告,这在useStyles 中没有描述。警告说:

警告:Material-UI:提供给 classes 属性的键 label 未在 undefined 中实现。 您只能覆盖以下之一:root。

Codesandbox中的示例

我知道如果我在 useStyles 中取消注释 label: {},这个错误就会消失。但是我正在制作一个组件库,所以可能会传递Button组件的所有属性,正如here所描述的那样。

那么问题来了:

  1. 我真的需要列出Button的所有这些道具吗?
  2. 如何确定应用样式的优先级(组件useStyles 中的样式的优先级低于通过道具传递的类名)?

【问题讨论】:

    标签: javascript reactjs material-ui


    【解决方案1】:

    您可以只覆盖makeStyle 类,{ ...classes, label: 'my-color2' },为example

    export default function ContainedButtons(props) {
      const classes = useStyles(props);
    
      return (
        <div>
          <Button variant="contained" classes={{ ...classes }}>
            Default
          </Button>
          <Button variant="contained" classes={{ ...classes, label: 'my-color2' }}>
            Default
          </Button>
        </div>
      );
    }
    

    【讨论】:

    • 谢谢,但这并不能解决问题。我仍然需要在 makeStyles 列出 label 道具(尝试在您的示例中删除它们并且您有警告)。但是我不想这样做,否则我可以列出Button组件的所有css道具
    • 你必须这样做,你会收到一个警告,因为你传递了&lt;Demo classes={{ label: "my-color", root: "my-margin" }} /&gt; 并且在应用于第一个按钮时没有这样的键。
    • 我知道了,所以除了列出所有属性别无他法?
    • 你可以通过Button实例,你想在哪里列出它们?在 makeStyles 中?
    • 如果我在 makeStyles 中至少将它们列为空对象,则不会发生错误。我的makeStyles 看起来像:makeStyles({root: {}, label: {}, text:{}, textPrimary: {} .....})。我觉得有点多余
    猜你喜欢
    • 2018-08-02
    • 2017-12-30
    • 2021-05-16
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 2021-10-21
    • 2018-09-25
    • 2019-10-30
    相关资源
    最近更新 更多