【问题标题】:MUI styled component, accessing inner css classesMUI 样式组件,访问内部 css 类
【发布时间】:2021-09-22 16:22:14
【问题描述】:

我正在尝试通过将样式传递给 Alert 组件的 icon CSS 规则来访问 MUI Alert 的内部 CSS。

import React from 'react';
import { makeStyles, Theme, createStyles } from '@material-ui/core/styles';
import Alert from '@material-ui/lab/Alert';
import { styled } from "@material-ui/core/styles";
import { spacing } from "@material-ui/system";

const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    icon: {
      marginTop: theme.spacing(2),
    },
  }),
);

const MyAlert = styled(Alert)(spacing);

export default function SimpleAlerts() {
  const styles = useStyles();

  return (
    <div>
      <MyAlert classes={{icon: styles.icon}}>This is an error alert — check it out!</MyAlert>
    </div>
  );
}

但即使样式工作正常,控制台仍会抱怨

Material-UI: The key `icon` provided to the classes prop is not implemented in WithStyles(ForwardRef(Alert)).

如何避免这个(控制台)错误?

Code Sandbox

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    看来你需要做的就是替换

    classes={{icon: styles.icon}}className={styles.icon}

    这有帮助吗?

    【讨论】:

    • 谢谢,这消除了错误,但样式也...
    猜你喜欢
    • 2021-12-09
    • 2022-07-19
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2022-01-25
    • 2017-10-14
    • 1970-01-01
    相关资源
    最近更新 更多