reactjs - 徽章组件的自定义颜色不起作用
问题描述
我需要为我的Badge
组件添加自定义颜色,但它似乎不起作用。
我试过这些:
<Badge className="badge" badgeStyle={{backgroundColor: '#00AFD7'}} variant="dot" />
<Badge className="badge" color='#00AFD7' variant="dot" />
这些不起作用。如何将自定义颜色传递给我的Badge
组件
解决方案
您可以利用withStyles
和使用badge
css 类来自定义它。
这是一个例子:
import React from "react";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";
import Badge from "@material-ui/core/Badge";
import MailIcon from "@material-ui/icons/Mail";
const styles = theme => ({
margin: {
margin: theme.spacing.unit * 2
},
customBadge: {
backgroundColor: "#00AFD7",
color: "white"
}
});
function SimpleBadge(props) {
const { classes } = props;
return (
<div>
<Badge
classes={{ badge: classes.customBadge }}
className={classes.margin}
badgeContent={10}
>
<MailIcon />
</Badge>
</div>
);
}
SimpleBadge.propTypes = {
classes: PropTypes.object.isRequired
};
export default withStyles(styles)(SimpleBadge);
在 v4 中,您可以在利用 props 的样式中使用函数。
此处的文档:https ://material-ui.com/styles/basics/#adapting-the-higher-order-component-api
const styles = theme => ({
margin: {
margin: theme.spacing.unit * 2
},
customBadge: {
backgroundColor: props => props.color,
color: "white"
}
});
推荐阅读
- flutter - 设备后退按钮无法正常工作
- javascript - 如何为amcharts添加圆角?
- laravel - 无法使用引导模型检索整个用户集合及其关系,我只收到单个用户及其关系
- reactjs - 使用带有 React Native 的 React Hooks 获取 API
- c# - 在比较 Linq 实体和 SQL 中的字符串时遇到问题
- c# - SQL 转换为 LINQ - 效率
- python - 在 OpenCV 中使用 Caffe 模型
- xml - 序列化自定义数据类型
- iot - 尽管我能够显式 ping 设备,但无法本地推送到设备(使用 balena)
- rust - 在为引用类型和非引用类型实现特征时,我是否必须两次实现特征?