reactjs - 更改 MUI 芯片原色或副色
问题描述
我正在尝试以编程方式更改 MUI 的颜色,Chip
但运气不佳。根据Chip
API,您必须通过 color 属性使用枚举中的三个值之一设置颜色;默认,主要和次要。然后您应该能够覆盖colorPrimary
or colorSecondary
css 类并且背景颜色应该改变。
这是我的代码示例:
<Chip key={label.id} color='primary' classes={{ colorPrimary: label.color }} label={label.label} />
以及浏览器中元素的图片: https : //i.imgur.com/bWYGzzz.png 还不能内联:(
如果您查看所选元素,您将看到我尝试应用的正确颜色#ff0000,因此它正在获取颜色并将其放置在某处。
我已经尝试过这种变体,添加了 colorBackground 属性,但是我收到一条错误消息,说 colorPrimary 类需要一个字符串而不是一个对象
<Chip key={label.id} color='primary' classes={{ colorPrimary: { backgroundColor: label.color } }} label={label.label} />
再次重申我的目标:我想将十六进制代码颜色应用于芯片以更改背景颜色。
解决方案
你可以通过多种方式做到这一点。
可以直接添加样式
<Chip key={label.id} color='primary' style={{backgroundColor:'green'}} label={label.label} />
或者您可以覆盖该类:
const StyleChip = withStyles({
root: {
backgroundColor:'salmon'
}
})(Chip);
在任何地方使用,您只会替换Chip
为StyleChip
<StyleChip key={label.id} color='primary' label={label.label} />
但是如果你想通过编程来上色,第一种方法是完美的,因为
style={{backgroundColor:_thisCanBeVariable_}}
推荐阅读
- java - 即使转到不同的 Activity,也可以从 Main Activity 继续 Fragment 中的媒体播放器(播放歌曲)
- keyboard - 绑定到键时,xdotool 无法在 i3wm 中工作
- java - Firebase 远程配置 - 安卓
- html - 滑块顶部不同设备尺寸的响应式徽标
- oracle - 如何创建在 oracle apex 报告中右键单击时出现的上下文菜单?
- python - 在 Pycharm Python virtualenv 中使用 subprocess.call() -- 如何为子进程停用 venv
- python - Django 表单:来自外键的引用字段
- docker - 卡夫卡经纪人认为没有经纪人(甚至没有经纪人)
- r - R绘图不保存为png,请检查我的代码
- machine-learning - 具有单个特征的梯度下降中的特征缩放