这些库都是在 React 应用中渲染 SVG 图标的解决方案,但它们在维护状态、图标集合范围、打包体积优化以及 API 设计上有显著差异。react-icons 是一个聚合库,支持包括 Bootstrap、Feather 和 FontAwesome 在内的多种图标集,并提供良好的树摇优化。react-bootstrap-icons 和 react-feather 是特定设计系统的官方或社区移植版本,适合风格统一的项目。react-fontawesome 是 FontAwesome 的早期 React 封装(现代项目推荐使用 scoped 版本),而 react-icons-kit 则是一个较旧的聚合方案,目前维护频率较低。
在 React 生态中,图标不仅仅是图片,它们是组件。选择合适的图标库会影响项目的打包体积、加载性能以及长期的维护成本。我们将深入对比 react-bootstrap-icons、react-feather、react-fontawesome、react-icons 和 react-icons-kit,看看它们在真实工程场景中的表现。
依赖管理是工程化的第一步。不同的库对依赖的要求不同,有的需要单独安装图标集,有的则是一站式解决。
react-bootstrap-icons 只需要安装主包,所有图标都包含在内。
npm install react-bootstrap-icons
react-feather 同样只需要安装主包,无需额外配置。
npm install react-feather
react-fontawesome 是 legacy 包,现代项目应使用 scoped 版本,但此处展示原包安装。
npm install react-fontawesome
# 注意:新项目建议 npm install @fortawesome/react-fontawesome
react-icons 安装一次即可使用所有支持的图标集,无需为每个集合单独安装包。
npm install react-icons
react-icons-kit 需要安装主包以及具体的图标集包,依赖管理较繁琐。
npm install react-icons-kit
npm install react-icons-kit/feather
导入方式直接影响代码的可读性和 IDE 的自动补全体验。我们看看如何渲染一个用户图标。
react-bootstrap-icons 使用命名导出,直接导入具体图标组件。
import { Person } from 'react-bootstrap-icons';
function App() {
return <Person size={24} color="currentColor" />;
}
react-feather 也是命名导出,API 非常直观。
import { User } from 'react-feather';
function App() {
return <User size={24} color="currentColor" />;
}
react-fontawesome 使用默认导出组件,通过属性传递图标名称(Legacy API)。
import FontAwesomeIcon from 'react-fontawesome';
function App() {
return <FontAwesome name="user" size="2x" />;
}
react-icons 需要从具体的图标集子路径导入,支持树摇。
import { FaUser } from 'react-icons/fa';
function App() {
return <FaUser size={24} color="currentColor" />;
}
react-icons-kit 需要导入 Icon 包装器以及具体的图标定义。
import { Icon } from 'react-icons-kit';
import { user } from 'react-icons-kit/feather/user';
function App() {
return <Icon size={24} icon={user} />;
}
图标通常需要调整大小、颜色或添加类名。不同库对这些属性的支持程度不同。
react-bootstrap-icons 支持标准的 SVG 属性,如 size 和 color。
<Person
size={32}
className="custom-icon"
style={{ fill: 'red' }}
/>
react-feather 同样支持标准 SVG 属性,透传能力强。
<User
size={32}
className="custom-icon"
stroke="red"
/>
react-fontawesome 使用专有属性来控制样式,如 size 和 className。
<FontAwesome
name="user"
size="2x"
className="custom-icon"
/>
react-icons 作为 React 组件,支持所有标准 SVG 属性和 props。
<FaUser
size={32}
className="custom-icon"
color="red"
/>
react-icons-kit 通过 Icon 组件的 props 来控制样式。
<Icon
size={32}
className="custom-icon"
icon={user}
color="red"
/>
在生产环境中,只打包用到的图标至关重要。这直接影响首屏加载速度。
react-bootstrap-icons 支持树摇,但需要配置好打包工具(如 Webpack/Vite)。
// 只导入用到的图标,未使用的会被剔除
import { Person } from 'react-bootstrap-icons';
react-feather 天然支持树摇,因为每个图标都是独立的导出。
// 打包工具只会包含 User 图标
import { User } from 'react-feather';
react-fontawesome 旧版本对树摇支持较差,容易打包进整个字体库。
// 容易引入多余资源,建议配合 SVG Core 使用
import FontAwesomeIcon from 'react-fontawesome';
react-icons 设计初衷就是为了树摇,按子路径导入效果最好。
// 明确指定子路径,确保只打包 fa 集合中的 User
import { FaUser } from 'react-icons/fa';
react-icons-kit 支持树摇,但依赖于具体的图标集包是否做了优化。
// 需要确保图标集包本身支持模块化
import { user } from 'react-icons-kit/feather/user';
选择库就是选择未来的维护成本。活跃的社区意味着更快的 Bug 修复和新图标支持。
react-bootstrap-icons 由 Bootstrap 社区维护,随 Bootstrap 图标更新而更新,稳定性高。
// 定期同步 Bootstrap 官方图标库
// 适合长期维护的企业级项目
react-feather 维护稳定,但图标集固定,不再新增大量新图标。
// 适合风格固定的项目
// 社区贡献主要集中在 Bug 修复
react-fontawesome 已过时,官方不再推荐,新功能缺失。
// ⚠️ 警告:此包已不再积极维护
// 建议迁移至 @fortawesome/react-fontawesome
react-icons 社区非常活跃,几乎每周都有更新,支持数百个图标集。
// 持续新增图标集支持
// 遇到问题容易找到社区解决方案
react-icons-kit 更新频率低,部分图标集可能过时。
// ⚠️ 注意:维护频率较低
// 新项目建议优先考虑 react-icons
| 特性 | react-bootstrap-icons | react-feather | react-fontawesome | react-icons | react-icons-kit |
|---|---|---|---|---|---|
| 图标集 | Bootstrap 官方 | Feather 单一集 | FontAwesome (旧) | 多集合聚合 | 多集合聚合 |
| 导入方式 | 命名导出 | 命名导出 | 默认导出 + 名称 | 命名导出 (子路径) | 包装器 + 图标对象 |
| 树摇支持 | ✅ 支持 | ✅ 支持 | ⚠️ 较弱 | ✅ 优秀 | ✅ 支持 |
| 维护状态 | 🟢 活跃 | 🟢 稳定 | 🔴 过时/遗留 | 🟢 非常活跃 | 🟡 低频 |
| 适用场景 | Bootstrap 项目 | 极简风格 | 遗留系统 | 通用/混合风格 | 遗留系统 |
react-icons 是目前最稳妥的通用选择 🧰。它解决了多图标集混用的痛点,并且树摇优化做得最好。如果你的项目没有特殊的设计系统限制,首选它。
react-bootstrap-icons 是 Bootstrap 用户的最佳伴侣 🔗。如果你已经在使用 Bootstrap CSS 框架,使用配套的图标库能保持视觉和代码的一致性。
react-feather 适合追求极致简洁的团队 🎨。如果设计稿严格遵循 Feather 风格,直接使用原包比通过聚合库导入更直接。
react-fontawesome 和 react-icons-kit 建议谨慎对待 ⚠️。除非是维护旧代码,否则在新架构中应避免引入这些维护频率较低或已过时的依赖,转而使用现代替代方案。
最终思考:图标库的选择不仅是技术决策,也是设计决策。确保你选择的库能支撑未来的设计变化,同时保持打包体积的可控性。
如果你需要在一个项目中混合使用多种图标风格,或者希望最大化利用树摇优化来减小体积,选择 react-icons。它支持几乎所有主流图标集,导入方式统一,社区活跃,更新及时,是目前大多数现代 React 项目的首选通用方案。
如果你偏好简洁、线条风格的图标,且希望依赖尽可能少的包,选择 react-feather。它专注于单一的高质量图标集,API 简单直接,适合注重极简设计风格的 C 端产品或轻量级应用,不需要额外配置即可使用。
如果你的项目严格遵循 Bootstrap 设计系统,或者你需要与 Bootstrap 组件库无缝配合,选择 react-bootstrap-icons。它提供了官方的 Bootstrap 图标集,命名规范与 Bootstrap 类名一致,维护状态良好,适合后台管理系统或依赖 Bootstrap 生态的项目。
除非你正在维护一个依赖此库的遗留系统,否则不建议在新项目中使用 react-icons-kit。它的维护频率远低于 react-icons,且需要为每个图标集单独安装包,增加了依赖管理的复杂度,现代工程实践中已有更优的替代方案。
仅当维护旧项目时使用 react-fontawesome。对于新项目,强烈建议迁移到 @fortawesome/react-fontawesome。如果你必须使用 FontAwesome 品牌图标库且无法迁移,请了解该包已过时,缺乏对新特性的支持,且可能存在安全风险,评估替代方案是更明智的选择。
Include popular icons in your React projects easily with react-icons, which utilizes ES6 imports that allows you to include only the icons that your project is using.
yarn add react-icons
# or
npm install react-icons --save
example usage
import { FaBeer } from "react-icons/fa";
function Question() {
return (
<h3>
Lets go for a <FaBeer />?
</h3>
);
}
View the documentation for further usage examples and how to use icons from other packages. NOTE: each Icon package has it's own subfolder under react-icons you import from.
For example, to use an icon from Material Design, your import would be: import { ICON_NAME } from 'react-icons/md';
Note This option has not had a new release for some time. More info https://github.com/react-icons/react-icons/issues/593
If your project grows in size, this option is available. This method has the trade-off that it takes a long time to install the package.
yarn add @react-icons/all-files
# or
npm install @react-icons/all-files --save
example usage
import { FaBeer } from "@react-icons/all-files/fa/FaBeer";
function Question() {
return (
<h3>
Lets go for a <FaBeer />?
</h3>
);
}
You can add more icons by submitting pull requests or creating issues.
You can configure react-icons props using React Context API.
Requires React 16.3 or higher.
import { IconContext } from "react-icons";
<IconContext.Provider value={{ color: "blue", className: "global-class-name" }}>
<div>
<FaFolder />
</div>
</IconContext.Provider>;
| Key | Default | Notes |
|---|---|---|
color | undefined (inherit) | |
size | 1em | |
className | undefined | |
style | undefined | Can overwrite size and color |
attr | undefined | Overwritten by other attributes |
title | undefined | Icon description for accessibility |
Import path has changed. You need to rewrite from the old style.
// OLD IMPORT STYLE
import FaBeer from "react-icons/lib/fa/beer";
function Question() {
return (
<h3>
Lets go for a <FaBeer />?
</h3>
);
}
// NEW IMPORT STYLE
import { FaBeer } from "react-icons/fa";
function Question() {
return (
<h3>
Lets go for a <FaBeer />?
</h3>
);
}
Ending up with a large JS bundle? Check out this issue.
From version 3, vertical-align: middle is not automatically given. Please use IconContext to specify className or specify an inline style.
<IconContext.Provider value={{ style: { verticalAlign: 'middle' } }}>
className StylingComponent
<IconContext.Provider value={{ className: 'react-icons' }}>
CSS
.react-icons {
vertical-align: middle;
}
Dependencies on @types/react-icons can be deleted.
yarn remove @types/react-icons
npm remove @types/react-icons
./build-script.sh will build the whole project. See also CI scripts for more information.
yarn
cd packages/react-icons
yarn fetch # fetch icon sources
yarn build
First, check the discussion to see if anyone would like to add an icon set.
https://github.com/react-icons/react-icons/discussions/categories/new-icon-set
The SVG files to be fetched are managed in this file. Edit this file and run yarn fetch && yarn check && yarn build.
https://github.com/react-icons/react-icons/blob/master/packages/react-icons/src/icons/index.ts
Note The project is not actively accepting PR for the preview site at this time.
The preview site is the react-icons website, built in Astro+React.
cd packages/react-icons
yarn fetch
yarn build
cd ../preview-astro
yarn start
The demo is a Create React App boilerplate with react-icons added as a dependency for easy testing.
cd packages/react-icons
yarn fetch
yarn build
cd ../demo
yarn start
SVG is supported by all major browsers. With react-icons, you can serve only the needed icons instead of one big font file to the users, helping you to recognize which icons are used in your project.
MIT