Font Awesome is a great resource to use various types of icons in your project including well-known social media icons and a lot more. It can be used with any front-end library. In this post, we are going to be using Font Awesome with React.
The first step usually is installing some NPM packages and in this case, it’s not so different. We will first install the core dependencies:
npm i --save @fortawesome/fontawesome-svg-core
npm i --save @fortawesome/react-fontawesome
BashNext up, we will install the packages for the icons themselves. Font Awesome comes with a collection of free icons that can be installed using:
# Free icons
npm i --save @fortawesome/free-regular-svg-icons
npm i --save @fortawesome/free-solid-svg-icons
Bashand they have a paid set of icons as well which can be installed using:
# Pro icons
npm i --save @fortawesome/pro-regular-svg-icons
npm i --save @fortawesome/pro-solid-svg-icons
npm i --save @fortawesome/pro-thin-svg-icons
npm i --save @fortawesome/pro-light-svg-icons
npm i --save @fortawesome/pro-duotone-svg-icons
npm i --save @fortawesome/sharp-solid-svg-icons
BashFor this post, we will be focussing on the free icons, though the setup is mostly going to be the same for pro icons with some checks for validation that we actually purchased them.
Note: to see which all icons are present, you can explore their gallery.
There are two ways of importing icons into your React project: individually and globally.
When adding icons individually, the icons are subsetted and optimized in the final bundle and you only include the icons that are imported into the bundle.
When doing it globally, all icons are imported in one go in an init
module and we do not need to individually import them into each component. This method can impact performance or we could end up including icons that are not being used in the project.
Let us take a look at how we can achieve both of these.
import ReactDOM from 'react-dom'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faCoffee } from '@fortawesome/free-solid-svg-icons'
const element = <FontAwesomeIcon icon={faCoffee} />
ReactDOM.render(element, document.body)
JavaScriptWhile this is a performant method than the global one, it can be a pain to add icons in different components.
We will add the global icons in our application’s entry point, such as the App.js file.
import ReactDOM from "react-dom";
import { library } from "@fortawesome/fontawesome-svg-core";
import { fab } from "@fortawesome/free-brands-svg-icons";
import { faCheckSquare, faCoffee } from "@fortawesome/free-solid-svg-icons";
library.add(fab, faCheckSquare, faCoffee);
JavaScriptThen, in our children components, we will use it as:
import React from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
export const Beverage = () => (
<div>
<FontAwesomeIcon icon="check-square" />
Your <FontAwesomeIcon icon="coffee" /> is hot and ready!
</div>
);
JavaScriptWe did not have to import individual icons in the file in this case. But the downside of this is that any icons imported globally are added to our bundle and it might increase the size of the bundle, even for routes that do not use the icons.
And those are the ways to use Font Awesome with React. We can choose from either way to do it depending on our project’s needs. Let us know in the comments if there is anything else that you would like to know about using Font Awesome with React.
I am terrible at optimizing my keyboard layout for anything. But off lately, my little…
I recently switched completely to the Brave browser and have set ad blocking to aggressive…
I was preparing a slide deck for a hackathon and decided to put in a…
I have been using npx a lot lately, especially whenever I want to use a…
Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is…
While working on a project, I wanted to do an integrity check of a file…