使用材质 ui createStyles

IT技术 reactjs typescript less material-ui jss
2021-04-28 21:52:47

我正在尝试将我的 css 从较少的文件转换为材质 createStyles,但我无法理解它是如何工作的。

我了解 createstyles 的基础知识,但我的子选择器无法正常工作

我想要的只是能够访问css类missionStatusLibelle

.missionStatus {
  display: flex;
  align-items: center;
  height: 34px;
  width: 100%;

  .missionStatusLibelle {
    align-items: flex-start;
    justify-content: flex-start;
    margin-left: 10px;
    font-size: 14px;
    font-weight: 500;
    line-height: 20px;
  }

}

喜欢

<div className={styles.missionStatusLibelle}>

并把它变成这样的东西

const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    missionStatus: {
      display: "flex",
      alignItems: "center",
      height: "34px",
      width: "100%",

      missionStatusLibelle: {
        alignItems: "flex-start",
        justifyContent: "flex-start",
        marginLeft: "10px",
        fontSize: "14px",
        fontWeight: 500,
        lineHeight: "20px"
      }
    }
}));

但是除了使用伪选择器之外,我无法进入missionStatusLibelle。

const styles = useStyles();
<div className={styles.missionStatusLibelle}>

1个回答

您需要类似以下内容:

const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    missionStatus: {
      display: "flex",
      alignItems: "center",
      height: "34px",
      width: "100%",

      "& $missionStatusLibelle": {
        alignItems: "flex-start",
        justifyContent: "flex-start",
        marginLeft: "10px",
        fontSize: "14px",
        fontWeight: 500,
        lineHeight: "20px"
      }
    },
    missionStatusLibelle: {}
  }
));

这是相关文档:https : //cssinjs.org/jss-plugin-nested/?v=v10.0.0-alpha.24#use-rulename-to-reference-a-local-rule-within- the- same-样式表