【问题标题】:Material UI Menu component is not getting the styles applied in reactMaterial UI Menu 组件没有在反应中应用样式
【发布时间】:2020-06-17 21:52:36
【问题描述】:

我是 material-UI 的新手,这里的菜单组件看起来像,

<Menu
    classes={{ paper: css.paperMenu }}
    className={classNames({
      [css.menuMarginTop]: true
    })}

>

.menuMarginTop {
  margin-top: 14px;
}

还有其他道具。现在,当我尝试在 prop 的基础上添加样式时,样式 menuMarginTop 没有被应用,谁能告诉我该怎么做?。

谢谢。

【问题讨论】:

  • 您尝试过建议的答案吗?

标签: css reactjs material-ui


【解决方案1】:

看起来你使用的是普通的 CSS,但是 material-UI 使用 JSS 底层作为他们的样式解决方案,你可以阅读那里的样式解决方案 here,以及如何自定义组件的 CSS here。

你可以这样做

import Menu from "@material-ui/core/Menu";
import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles(theme => ({
  paperMenu: {
    color: "red"
  },
  menuMarginTop: {
    color: "blue"
  }
}));

export default function App(props) {
  const classes = useStyles();
  return (
    <Menu
      classes={{ paper: classes.paperMenu }}
      className={classes.menuMarginTop}
    />
  );
}

这里,classes.paperMenu 将覆盖底层的纸张(组件)类,menuMarginTop 将应用于 Menu 组件的根元素。 Material-UI 在运行时随机生成唯一的类名,所以你不能像其他库一样依赖固定的类名,你只能使用 classes 属性覆盖底层类,更多信息在上面的链接中

请记住,这是覆盖类的方法之一,还有其他方法以及高阶组件withStyles、withTheme 等,上面的链接将为您提供足够的信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 2022-01-27
    • 2017-04-19
    • 2019-04-15
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多