我正在为SVG图标使用React组件,例如:
const ErrorIcon = props => {
const { color } = props;
return (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 90.206 94.8">
<path
d="M55.7 82.9v2.8c0 2.2-1."
fill={color}
/>
</svg>
);
};
我有一个页面,需要根据来自数据库的ID提供的名称应用图标
const data = [{ id: "dog", name: "A Dog" }, { id: "cat", name: "A kitty"}]
如果我是基于类名设计样式,那么我可以这样呈现ID:
const MyComponent = props => {
return (
<ul>
{
data.map(item=>{
return(<li className={`Icon Icon--${item.id}`} key={item.id}>{item.name}</li>)
})
}
</ul>
);
};
和样式:
.Icon--dog {
background: url(dog.jpg);
}
有没有一种方法可以做到这一点,同时保持我的图标作为React组件?