首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MUI5 sx支柱行为

MUI5 sx支柱行为
EN

Stack Overflow用户
提问于 2022-01-09 11:14:14
回答 1查看 379关注 0票数 0

我玩的材料UI5SX道具只是出于好奇,不知道如何使它类似于以前的MUI 4 makeStyles fn。我想把JSS类线虫传给sx道具。因此,在这个代码片段中,我想重写偶数字符串的颜色:

代码语言:javascript
复制
import { Typography } from "@mui/material";
const styles = {
  green: {
    color: 'green',
  },
  red: {
    color: 'red',
  },
  
}
const options = ['even', 'odd', 'even', 'odd']
export default function CssModulesSlider() {
  console.log('classes', styles)
  return (
    <>
    {options.map((item, index)=> {
      if(index %2 === 0){
        return (
          <Typography sx={{...styles.red}}>
              {item}
          </Typography>
        )
      } else {
       return (
        <Typography sx={{...styles.red, ...styles.green,}}>
            {item}
        </Typography>
       ) 
      }
      
    })}
    </>
  );
}

MUI将其编译成不同的CSS类:

代码语言:javascript
复制
.css-**1kivl2a**-MuiTypography-root {
//some mui typography stuff here
    color: red;
}
.css-**1ti676r**-MuiTypography-root {
//some mui typography stuff here
    color: green;
}

这个代码框示例里一切都很完美,

直到我更改了在sx道具中定义类的顺序:

代码语言:javascript
复制
//from
sx={{...styles.red, ...styles.green,}}
//to
sx={{...styles.green, ...styles.red,}} 

虽然样式对象仍然与您在console.log中看到的相同,但MUI将其全部编译为一个CSS类,因此所有列表项都具有相同的颜色: red规则。这与CSS规则覆盖无关,这取决于它的顺序,因为在第二种情况下,编译的CSS中的类是相同的

当然,使用CSS模块、情感或样式组件更好。但有人知道为什么会这样吗?

EN

回答 1

Stack Overflow用户

发布于 2022-01-09 14:49:52

这不是MUI,而是如何传播(.)作品

代码语言:javascript
复制
const styles = {
  green: {
    color: 'green',
  },
  red: {
    color: 'red',
  }, 
}

console.log('...red + ...green => green', {...styles.red, ...styles.green});
console.log('...green + ...red => red', {...styles.green, ...styles.red});

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70640718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档