🫱How to set up Font Awesome in React
https://dev.to/davidemaye/how-to-set-up-font-awesome-in-react-5a8d
src\App.tsx
How to set up Font Awesome in React
#react#fontawesome#icons#beginners
Hello There 😊
In this article, we will learn how to implement Font Awesome icons library in a React project.
Introduction
Prerequisites
To complete this tutorial, you will need the following:
A basic understanding of React before starting this tutorial.
Node.js installed locally. You can do so here; Install Node.js.
We will achieve our aim of this article by doing the following.
Setting up our react project.
Installing font awesome's SVG core package.
Installing font awesome's icon packages (We'll be using the Free icons).
Installing the font awesome react component.
Importing icons into each component.
Importing icons globally.
Setting up our react project
First, open your Visual Studio code's terminal or device's terminal and type the following command: npx create-react-app font-awesome-react
Next, we will open the App.js
file in the src folder, remove the original React element rendered there and replace it with any other element of your choice.
Installing font awesome's SVG core package
We will need to install the core package, which includes all the utilities, to make the icons work.
Installing font awesome's icon packages
Next, we will install the icon packages in different styles: solid, regular, light, thin, and duotone.
Not all icons are free, so you can install the icon packages for the free icons or the pro icons.
Free Icons
These are the styles available for the free icons.
Pro Icons
You must have a valid Pro Package Token and an active Pro Plan subscription to configure Pro access for your project if you intend to use the Pro packages, which offer additional icons and styles.
These are the styles available for the pro icons.
Installing the font awesome react component
Now we will install the Font Awesome React component.
After installing all the necessary packages above, we will navigate to our package.json
file to see everything we have installed.
Importing icons into each component
For the purpose of this article, we will be creating a new page: IconPage.js
, which we will then import into our App.js
.
We would import some icons into the IconPage.js
file.
To do this, we have to import FontAwesomeIcon
into the file.
Then, we can add any free icon we need to the project by importing the icon from the package we installed earlier. e.g:
import { faPenNib } from '@fortawesome/free-solid-svg-icons'
You can import them together in a single line.
...or you can import them globally, which we will discuss below.
Importing icons globally
To import icons globally, we will navigate to the App.js
file. We will then import a library
from the SVG core package we installed.
Remember, we talk about the icons having different styles. We would be importing every icon style. First, we have to know how these styles are represented.
Solid Style -
fas
Regular Style -
far
Light Style -
fal
Duotone Style -
fad
Brand Style -
fab
So we will be importing each style with what they are represented with.
We imported just these three because they are the only styles available for the free icon.
We will add all the imported styles to our library by adding this code below export default App;
in the App.js
file.
This is how the App.js
file will be now.
Doing these, we have added Font Awesome Icons globally.
Now, let's see how to use the icons in the new file we will create. IconPageGlobal.js
.
In the IconPageGlobal.js
created, we will only import FontAwesomeIcon
by typing:
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
To use the icons:
This is the IconPageGlobal.js
file now.
Conclusion
Importing icons globally is not recommended because it can make your package larger by including icons you won't be using. It is advised to import Font Awesome icons one at a time so that your final package sizes are as little as possible because you will only import the icons that you actually need. To access the codes on this articles check this repository
Please leave a comment below to ask me anything! I’m always happy to talk and help.
Last updated
Was this helpful?