【发布时间】:2020-05-04 21:57:04
【问题描述】:
当提供颜色道具时,我正在尝试将 svg 图标的颜色从黑色更改为白色。
export class CategoryIconComponent extends React.Component {
static displayName = 'CategoryIcon';
state = { icon: null };
static propTypes = {
title: PropTypes.string,
color: PropTypes.string,
};
static defaultProps = {
title: null,
color: '#fff',
};
componentDidMount() {
const { title } = this.props;
if (getTopCategoryIcon('Concrete Contractors') != null){
if(typeof getTopCategoryIcon('Concrete Contractors') === 'string'){
this.setState({ icon: getTopCategoryIcon('Concrete Contractors') });
}
else{
getTopCategoryIcon(title).then((newIcon) => {
this.setState({ icon: newIcon });
});
}
}
}
render() {
const { icon } = this.state;
const { color } = this.props;
return (
icon && (
<TextIconContainer>
// I want to be able to change color of the icon from black to white via color prop here
// something like
<img src={icon} width={25} height={25} color={color} />
</TextIconContainer>
)
);
}
有没有 CSS 方法可以做到这一点?或任何其他方式我可以利用 svg 组件并更改它?
【问题讨论】:
标签: javascript css reactjs svg ecmascript-6