2018-07-19 19:24:19 +08:00
---
order: 1
title:
zh-CN: 自定义图标
en-US: Custom Icon
---
## zh-CN
2018-08-20 17:23:28 +08:00
利用 `Icon` 组件封装一个可复用的自定义图标。可以通过 `component` 属性传入一个组件来渲染最终的图标,以满足特定的需求。这个例子中使用了 `@svgr/webpack` 来将 `svg` 图标转化为 `React` 组件。
2018-07-19 19:24:19 +08:00
## en-US
2018-08-20 17:23:28 +08:00
Create a reusable React component by using `<Icon component={...} />` . The property `component` takes a React component that renders to `svg` element. This demo shows how to convert `svg` icon to a React component by using `webpack` and loader `@svgr/webpack` .
2018-07-19 19:24:19 +08:00
````jsx
import { Icon } from 'antd';
2018-08-15 17:21:02 +08:00
import HeartSvg from './assets/heart.svg';
import AntDesignSvg from './assets/ant-design.svg';
// use webpack loader `@svgr/webpack` ,
2018-08-20 17:23:28 +08:00
// which converts the `*.svg` file into a React component.
2018-07-19 19:24:19 +08:00
2018-08-15 17:21:02 +08:00
const HeartIcon = props => (
2018-08-16 11:41:37 +08:00
< Icon component = {HeartSvg} { . . . props } / >
2018-08-15 17:21:02 +08:00
);
2018-07-19 21:07:33 +08:00
2018-08-15 17:21:02 +08:00
const AntDesignIcon = props => (
2018-08-16 11:41:37 +08:00
< Icon component = {AntDesignSvg} { . . . props } / >
2018-08-15 17:21:02 +08:00
);
ReactDOM.render(
< div className = "custom-icons-list" >
< HeartIcon style = {{ color: ' hotpink ' } } / >
< AntDesignIcon style = {{ fontSize: ' 32px ' } } / >
2018-07-19 19:24:19 +08:00
< / div > ,
mountNode
);
````
2018-08-14 17:17:51 +08:00
```css
2018-08-15 17:21:02 +08:00
.custom-icons-list > .anticon {
2018-08-14 17:17:51 +08:00
margin-right: 6px;
}
```