【发布时间】:2020-03-01 09:43:02
【问题描述】:
你好,我是一个反应新手,对造型也很陌生 :) 我正在尝试使用样式化组件来设置 material-ui Button 组件的样式
我通过覆盖全局类名来做到这一点,我知道 material-ui 引入了全局类名,例如 MuiButton-root 等
"&"在父选择器中的使用我不清楚,例如:
const StyledButton = styled(Button)`
&.MuiButton-root {
width: 500px;
}
.MuiButton-label {
color: ${props => props.labelColor};
justify-content: center;
}
`;
以上代码有效,可以实现以下效果:
- 按钮的宽度为 500 像素
- 标签颜色为红色(labelColor 作为道具传入)
- 完整代码请参见下面的沙箱
问题: 为什么 MuiButton-root 需要“&”选择器,而 MuiButton-label 不需要?
这也是用样式化的组件来设置 material-ui 样式的最佳方式吗?
【问题讨论】:
标签: reactjs material-ui styled-components