There are a few ways of adding icons to a React project. Choose the option that works for your project, and then add icons in your UI using the FontAwesomeIcon element.
There are a few ways to add icons when using React. The easiest way is to use a Pro Kit
which allows custom icon upload and icon subsetting. But you can choose other methods that allowyou to use icons from our SVG icon packages.
Using a Kit Package
If you’ve created a Kit and installed it in your project, you’re ready to get going.
By prefix and name
Font Awesome Icons
Custom Icons
For this to work, you’ll need to have a Kit that contains the icons in the examples. If you’re not familiar with how Kits work, you can find out here.
Importing specific icons
An alternative to using the prefix and name is by importing icons directly. This is your best bet at leveraging tree-shaking if that’s useful to you.
You can use all the icons in a family and style, too. But this will put the kibosh on tree-shaking (Probably? Are we using A.I. for this yet?).
Using the Library
Another mechanism that the SVG Core provides is a JavaScript class called Library.
With a subsetted Kit, this can be an easy way to add all icons once and use them with a syntax that requires less typing.
Now all icons in the Kit have been added in just one, easy line. No fuss, no muss.
Using it doesn’t require importing the icons. You just need an array or string.
As a string
As an array
Custom icons are just as easy.
Typescript and custom icons Issue
Currently there are some issues using custom icons with Typescript. We’ll beworking to address these in future versions of Font Awesome but for now, wehave a few workarounds.
Library using an array
Error
Workaround
Library using a string
Error
Workaround
We know this is annoying and defeats the purpose of using Typescript. We’reworking on it, but we think we’ll have to reach forGenerics to fixit and we didn’t want to hold up the original release of Kit Packages.
Add Some Style
Now that you have some icons on the page, add some pieces of flair! Check out all the styling options you can use with Font Awesome and React.
Express Yourself with Some Styling!
Importing from SVG Icon Packages
If you can’t or don’t want to use a Kit, you can explicitly add individual icons to each component. Here’s a simple example:
Add Individual Icons Explicitly
Notice that the faEnvelope icon is imported from @fortawesome/free-solid-svg-icons as an object and then provided to the icon prop as an object.
Add Icons Globally
We like to travel light so we don’t recommend this method unless you know what you’re doing. Globally importing icons can increase the size of your bundle with icons you aren’t using. It also couples your components to another module that manages your icons.
First, you’ll import the icons you want to use via a “library” in the initializing module of your React application, like App.js. Here’s an example of that:
In our call to library.add() we’re passing:
fas: which represents all of the icons in @fortawesome/free-solid-svg-icons. (Be careful importing whole styles - it can be a LOT of icons!) So any of the icons in that package may be referenced by icon name as a string anywhere else in our app. For example: coffee, check-square, or spinner.
faTwitter, faFontAwesome, faHatCowboy, faHatChef, and faPlateUtensils: Adding each of these icons individually allows us to refer to them throughout our app by their icon string names, twitter, font-awesome, hat-cowboy, hat-chef, and plate-utensils.
You can then use any of those icons anywhere in your app without needing to re-import into each component. So if you used icons in a couple of components, that would end up looking something like this:
You’ll notice we were able use the imported brand icons without explicitly importing them in the component. And we used the square-check, and envelope icons without explicitly importing them anywhere. But, our bundle now has over 1000 solid icons plus the two brand icons we added, which is more than we’re using - a good reason to avoid importing a whole style.
Same icons, Different Styles
Using ES modules and import statements we can define unique names for two different styles of the same icon. Here’s an example:
What about Dynamic Importing Deprecated
Install the Babel Macros
First, you’ll install the babel macros using npm or yarn:
Set Up the Babel Configs
Next, you’ll need to configure the babel plugins. Add the following to your babel.config.js file:
Then, create a babel-plugin-macros.config.js and add the fontawesome-svg-core settings. You can set the license to either free or pro depending on the icons you are planning to use. (Learn more about setting babel macros)
Add the Icons to Your Project
Use the syntax below wherever you want them to appear in your project.
Seeing it in context makes more sense.
Older import macros solid(), regular(), light(), thin(), duotone(),and brands() are still supported for backward-compatibility. But we recommendyou switch to the newer icon() function
To use Font Awesome icons in your React project, you need to install the fortawesome/fontawesome-svg-core, fortawesome/free-solid-svg-icons, fortawesome/react-fontawesome, and fortawesome/free-brands-svg-icons packages. The fortawesome/fontawesome-svg-core package is the core package of Font Awesome.
import React from 'react'; import { ReactComponent as MyLogo } from './my_logo.svg'; const MyComponent = () => { return ( <div> <MyLogo /> </div> ); }; export default MyComponent; In the above example, we import an SVG image using the ReactComponent syntax.
React icons library is a great resource for React developers, who can use its customizable SVG icons in their applications. It offers an extensive library of icons to choose from, which can be easily inserted into projects with just a few lines of code.
To add an item or multiple items, addItems method can be used. In the following example, the Bugatti Veyron Super Sport and SSC Ultimate Aero items will be added while clicking Add Items button.
Using via Google Fonts. Similar to other Google Web Fonts, the correct CSS will be served to activate the 'Material Icons' font specific to the browser. An additional CSS class will be declared called .material-icons . Any element that uses this class will have the correct CSS to render these icons from the web font.
To place the icon on a menu item, set the iconCss property with the required icon CSS. By default, the icon is positioned at the left of the menu item. In the following sample, the icons of File and Edit menu items and Open , Save , Cut , Copy ,and Paste sub menu items are added using the iconCss property.
To place the icon on a menu item, set the iconCss property with the required icon CSS. By default, the icon is positioned at the left of the menu item. In the following sample, the icons of File and Edit menu items and Open , Save , Cut , Copy ,and Paste sub menu items are added using the iconCss property.
You can render icons to the list items by mapping the iconCss field. This iconCss field create a span in the list item with mapped class name to allow styling as per your need.
Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.
We notice you're using an ad blocker
Without advertising income, we can't keep making this site awesome for you.