React

Using Font Awesome with React

Advertisements

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.

Set up

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
Bash

Next 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
Bash

and 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
Bash

For 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.

Using Font Awesome with React

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.

Individually importing icons

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)
JavaScript

While this is a performant method than the global one, it can be a pain to add icons in different components.

Globally importing icons

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);
JavaScript

Then, 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>
);
JavaScript

We 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.

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCR which is a developer community in Delhi, India.

Share
Published by
Saransh Kataria

Recent Posts

Remapping keyboard keys to avoid Carpal Tunnel

I am terrible at optimizing my keyboard layout for anything. But off lately, my little…

1 month ago

Fixing cookies are blocked for a website with shields down on Brave

I recently switched completely to the Brave browser and have set ad blocking to aggressive…

5 months ago

Generating a QR code using Node.js

I was preparing a slide deck for a hackathon and decided to put in a…

6 months ago

How to clear the global npx cache

I have been using npx a lot lately, especially whenever I want to use a…

7 months ago

Copy/Pasting output from the terminal

Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is…

7 months ago

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

8 months ago
Advertisements