What is react-icons?
The react-icons package provides a large collection of icons from various icon libraries that can be easily used in React applications. It allows developers to include icons using React components, which can be styled and manipulated like any other React component.
What are react-icons's main functionalities?
Importing and using an icon
This feature allows you to import an icon from a specific icon library (in this case, Font Awesome) and use it as a React component in your application.
import { FaBeer } from 'react-icons/fa';
function App() {
return <h3> Lets go for a <FaBeer />? </h3>;
}
Styling icons
Icons can be styled using inline styles or CSS classes, just like any other React component. This code sample demonstrates inline styling to change the color and size of the icon.
import { FaBeer } from 'react-icons/fa';
function App() {
return <FaBeer style={{ color: 'blue', fontSize: '50px' }} />;
}
Combining icons with other components
React-icons can be easily combined with other components, such as buttons from a UI library like React Bootstrap. This allows for more complex and visually appealing components.
import { FaBeer } from 'react-icons/fa';
import { Button } from 'react-bootstrap';
function App() {
return (
<Button variant='primary'>
Order a beer <FaBeer />
</Button>
);
}
Other packages similar to react-icons
mdi-react
This is a set of Material Design Icons for React. It is similar to react-icons in that it provides a large set of icons, but it is focused solely on Material Design Icons, whereas react-icons offers icons from multiple libraries.
react-fontawesome
This package is a React component for Font Awesome icons. It is similar to react-icons, but it is dedicated to Font Awesome icons only. react-icons, on the other hand, includes Font Awesome among many other icon libraries.
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.
Installation (for standard modern project)
yarn add react-icons
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';
Installation (for meteorjs, gatsbyjs, etc)
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
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>
)
}
Icons
You can add more icons by submitting pull requests or creating issues.
Configuration
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 |
Migrating from version 2 -> 3
Change import style
Import path has changed. You need to rewrite from the old style.
import FaBeer from 'react-icons/lib/fa/beer';
function Question(){
return (
<h3> Lets go for a <FaBeer />? </h3>
)
}
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.
Adjustment CSS
From version 3, vertical-align: middle
is not automatically given. Please use IconContext to specify className or specify an inline style.
Global Inline Styling
<IconContext.Provider value={{ style: { verticalAlign: 'middle' } }}>
Global className
Styling
Component
<IconContext.Provider value={{ className: 'react-icons' }}>
CSS
.react-icons {
vertical-align: middle;
}
TypeScript native support
Dependencies on @types/react-icons
can be deleted.
Yarn
yarn remove @types/react-icons
NPM
npm remove @types/react-icons
Contributing
Development
yarn
yarn submodule
cd packages/react-icons
yarn build
Preview
The preview site is the react-icons
website, built in NextJS.
cd packages/react-icons
yarn build
cd ../preview
yarn start
Demo
The demo is a Create React App boilerplate with react-icons
added as a dependency for easy testing.
cd packages/react-icons
yarn build
cd ../demo
yarn start
Why React SVG components instead of fonts?
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.
Related Projects
Licence
MIT
- Icons are taken from the other projects so please check each project licences accordingly.