Add Icons with React
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.
About Adding Icons in React
Section titled “About Adding Icons in React”There are a bunch of ways to add icons in a React project. Which one is right for you depends on how you set up your project and how you plan to use icons. Here is a quick overview of the options — the details on how to use each are below.
Type | Method | Pros | Cons |
---|---|---|---|
Kit Pkg Best Pro Option | ByPrefixAndName | Easiest, and lightweight when you subset your Kit | Not centralized, can be bulky if you don’t subset your Kit |
Kit Pkg | Individual Icons | Easy to see which icons are imported, Allows tree-shaking | Not centralized, tedious to add many icons, can be ambiguous about style |
Kit Pkg | Whole Styles | Fast way to add lots of icons | Not centralized, bulky |
Kit Pkg | Use the Library | Import all the icons in your Kit once, use anywhere in your Vue project | Can be bulky if you don’t subset your Kit |
SVG Pkg | Individual Icons | Easy to see which icons are imported | Tedious to add many icons, can be ambiguous about style |
SVG Pkg Best Free Option | Whole Styles (Free) | Easy way to add all the Free icons | Only includes Free icons |
SVG Pkg | Whole Styles (Pro) | Fast way to add lots of icons | Can be very bulky, doesn’t work with tree-shaking |
Add Icons Using a Kit Package
Section titled “Add Icons Using a Kit Package”The easiest way to add icons when using React is with a Pro Kit package — which lets you add your own custom icons and create a custom subset with just the icons or styles you need. Check out the Kit Package API docs to find out what’s in the package and learn more about using one.
And don’t forget - for the examples below to work, you’ll need to make sure your Kit contains the icons in the examples, especially if you’ve subsetted your Kit. If you’re not familiar with how Kits work, you can learn more about Kits.
By Prefix and Name
Section titled “By Prefix and Name”import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'import { byPrefixAndName } from '@awesome.me/kit-KIT_CODE/icons'
function App() { return ( <div> {/* Font Awesome Icon */} <FontAwesomeIcon icon={byPrefixAndName.fas['house']} /> <FontAwesomeIcon icon={byPrefixAndName.far['circle-user']} />
{/* Custom Icon */} <FontAwesomeIcon icon={byPrefixAndName.fak['my-icon']} /> </div> );}
export default App;
Importing Specific Icons
Section titled “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.
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'import { faHouse } from '@awesome.me/kit-KIT_CODE/icons/classic/solid'import { faCircleUser } from '@awesome.me/kit-KIT_CODE/icons/classic/regular'import { faCat } from '@awesome.me/kit-KIT_CODE/icons/sharp/solid'import { faDog } from '@awesome.me/kit-KIT_CODE/icons/duotone/solid'import { faDragon } from '@awesome.me/kit-KIT_CODE/icons/sharp-duotone/solid'import { faMyIcon } from '@awesome.me/kit-KIT_CODE/icons/kit/custom'
function App() { return ( <div> {/* Font Awesome Icons */} <FontAwesomeIcon icon={faHouse} /> <FontAwesomeIcon icon={faCircleUser} /> <FontAwesomeIcon icon={faCat} /> <FontAwesomeIcon icon={faDog} /> <FontAwesomeIcon icon={faDragon} />
{/* Custom Icon */} <FontAwesomeIcon icon={faMyIcon} /> </div> );}
export default App;
Importing Whole Styles
Section titled “Importing Whole Styles”You can use all the icons in a style, but this method won’t work with tree-shaking and can add bloat to your project since it includes all the icons in the style so use it only when the other methods don’t work for your project.
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'import { fas, far, fad, fass, fasds, fak } from '@awesome.me/kit-KIT_CODE/icons'
function App() { return ( <div> {/* Font Awesome Icons */} <FontAwesomeIcon icon={fas.faHouse} /> <FontAwesomeIcon icon={far.faCircleUser} /> <FontAwesomeIcon icon={fad.faMouse} /> <FontAwesomeIcon icon={fass.faCat} /> <FontAwesomeIcon icon={fasds.faDog} />
{/* Custom Icon */} <FontAwesomeIcon icon={fak.faMyIcon} /> <FontAwesomeIcon icon={fak.faAnotherOne} /> <FontAwesomeIcon icon={fak.faMyLogo} /> </div> );}
export default App;
Using the Library
Section titled “Using the Library”Another mechanism that the Font Awesome 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.
First set up the Library:
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'import { library } from '@fortawesome/fontawesome-svg-core'import { all } from '@awesome.me/kit-KIT_CODE/icons'
library.add(...all)
Now all icons in the Kit have been added in just one, easy line. No fuss, no muss. But you’ll want to make sure your Kit is using subsetting so you aren’t loading tons of icons you’re not using.
A nice benefit of this method is that you can just add the icon as an array or string in your templates without having to import the icons individually.
<FontAwesomeIcon icon="fa-solid fa-house" /><FontAwesomeIcon icon="fa-regular fa-circle-user" />
<FontAwesomeIcon icon={['fas', 'house']} /><FontAwesomeIcon icon={['far', 'circle-user']} />
Custom icons are just as easy.
<FontAwesomeIcon icon="fa-kit fa-my-icon" />
<FontAwesomeIcon icon={['fak', 'my-icon']} />
TypeScript and Custom Icons
Section titled “TypeScript and Custom Icons”import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'import { library } from '@fortawesome/fontawesome-svg-core'import { all } from '@awesome/kit-KIT_CODE/icons'
library.add(...all)
const element = <FontAwesomeIcon icon="fa-kit fa-my-icon" />// This will cause this TypeScript error: `Type is not assignable to type IconProp`
ReactDOM.render(element, document.body)
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'import { library } from '@fortawesome/fontawesome-svg-core'import { all } from '@awesome/kit-KIT_CODE/icons'
library.add(...all)
const element = <FontAwesomeIcon icon="{['kit', 'my-icon']}" />// This will cause this TypeScript error: `Type is not assignable to type IconProp`
ReactDOM.render(element, document.body)
Workaround
Section titled “Workaround”import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'import { library, IconProp } from '@fortawesome/fontawesome-svg-core'import { all } from '@awesome/kit-KIT_CODE/icons'
library.add(...all)
// @ts-ignoreconst myIcon : IconProp = "fa-kit fa-my-icon"
const element = <FontAwesomeIcon icon={myIcon} />
ReactDOM.render(element, document.body)
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'import { library, IconProp } from '@fortawesome/fontawesome-svg-core'import { all } from '@awesome/kit-KIT_CODE/icons'
library.add(...all)
// @ts-ignoreconst myIcon : IconProp = ['kit', 'my-icon']
const element = <FontAwesomeIcon icon={myIcon} />
ReactDOM.render(element, document.body)
We know this is annoying and defeats the purpose of using TypeScript. We’re working on it, but we think we’ll have to reach for Generics to fix it and we didn’t want to hold up Kit Packages.
Add Icons Using SVG Icon Packages
Section titled “Add Icons Using SVG Icon Packages”If you can’t or don’t want to use a Kit, you can add icons into your React project components using our SVG packages by style. (Remember: Pro+ and Custom icons aren’t available with SVG Packages - you’ll need to use a Kit package to use those icons.)
Add Individual Icons
Section titled “Add Individual Icons”You can add each icon individually to keep your bundle small and efficient if you’re only using a few icons. Here’s an example using our SVG Icon Packages:
import ReactDOM from 'react-dom'import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'import { faEnvelope } from '@fortawesome/free-solid-svg-icons'
const element = <FontAwesomeIcon icon={faEnvelope} />
ReactDOM.render(element, document.body)
Notice that the faEnvelope
icon is imported from the Free Solid style pacakge - @fortawesome/free-solid-svg-icons
- and is imported as an object and then provided to the icon
prop as an object.
Same Icons, Different Styles
Section titled “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 with different styles of the “house” icon:
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'import { library } from '@fortawesome/fontawesome-svg-core'
import { faHouse as fasFaHouse } from '@fortawesome/pro-solid-svg-icons'import { faHouse as fadFaHouse } from '@fortawesome/pro-duotone-svg-icons'import { faHouse as fadtFaHouse } from '@fortawesome/duotone-thin-svg-icons'import { faHouse as fassFaHouse } from '@fortawesome/sharp-solid-svg-icons'import { faHouse as fasdsFaHouse } from '@fortawesome/sharp-duotone-solid-svg-icons'import { faCircleUser as farFaCircleUser } from '@fortawesome/pro-regular-svg-icons'import { faCircleUser as falFaCircleUser } from '@fortawesome/pro-light-svg-icons'
library.add(fasFaHouse, fadFaHouse, fadtFaHouse, fassFaHouse, fasdsFaHouse, farFaCircleUser, falFaCircleUser)
And here’s how you would add the “house” icon in various styles into your component:
<FontAwesomeIcon icon="fa-solid fa-house" /><FontAwesomeIcon icon="fa-duotone fa-solid fa-house" /><FontAwesomeIcon icon="fa-duotone fa-thin fa-house" /><FontAwesomeIcon icon="fa-sharp fa-solid fa-house" /><FontAwesomeIcon icon="fa-sharp-duotone fa-solid fa-house" /><FontAwesomeIcon icon="fa-regular fa-circle-user" /><FontAwesomeIcon icon="fa-light fa-circle-user" />
<FontAwesomeIcon icon="['fas', 'house']" /><FontAwesomeIcon icon="['fad', 'house']" /><FontAwesomeIcon icon="['fadt', 'house']" /><FontAwesomeIcon icon="['fass', 'house']" /><FontAwesomeIcon icon="['fasds', 'house']" /><FontAwesomeIcon icon="['far', 'circle-user']" /><FontAwesomeIcon icon="['fal', 'circle-user']" />
Add Whole Styles
Section titled “Add Whole Styles”Free only
Section titled “Free only”If you’re just using the Free icons and styles, the easiest way to get the icons into your app is to add the Free SVG Icon Packages. Here’s an example of how to do that:
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'import { library } from '@fortawesome/fontawesome-svg-core'
/* import all the icons in Free Solid, Free Regular, and Brands styles */import { fas } from '@fortawesome/free-solid-svg-icons'import { far } from '@fortawesome/free-regular-svg-icons'import { fab } from '@fortawesome/free-brands-svg-icons'
library.add(fas, far, fab)
And then you can use any of the icons in those styles without having to explictly import them, like this:
<template> <FontAwesomeIcon icon="fa-solid fa-dog" /> <FontAwesomeIcon icon="fa-regular fa-circle-user" /> <FontAwesomeIcon icon="fa-brands fa-threads" /><template>
<template> <FontAwesomeIcon icon="['fas', 'dog']" /> <FontAwesomeIcon icon="['far', 'circle-user']" /> <FontAwesomeIcon icon="['fab', 'threads']" /></template>
Add Whole Pro Styles
Section titled “Add Whole Pro Styles”You can also add all the icons in a style - like Sharp Regular or Duotone - but be careful as each Pro style has thousands of icons. Here’s an example of how to import whole styles:
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'import { library } from '@fortawesome/fontawesome-svg-core'
/* import all the icons in Free Solid, Duotone Solid, and Duotone Thin styles */import { fas } from '@fortawesome/free-solid-svg-icons'import { fad } from '@fortawesome/pro-duotone-svg-icons'import { fadt } from '@fortawesome/duotone-thin-svg-icons'
library.add(fas, fad, fadt)
And then you can use any of the icons in those styles without having to explictly import them, like this:
<template> <FontAwesomeIcon icon="fa-solid fa-dog" /> <FontAwesomeIcon icon="fa-duotone fa-solid fa-circle-user" /> <FontAwesomeIcon icon="fa-duotone fa-thin fa-fish" /><template>
<template> <FontAwesomeIcon icon="['fas', 'dog']" /> <FontAwesomeIcon icon="['fad', 'circle-user']" /> <FontAwesomeIcon icon="['fadt', 'fish']" /></template>
Add Icons Globally
Section titled “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:
import ReactDOM from 'react-dom'import { library } from '@fortawesome/fontawesome-svg-core'
import { fas } from '@fortawesome/free-solid-svg-icons'import { fass } from '@fortawesome/sharp-solid-svg-icons'import { fad } from '@fortawesome/pro-duotone-svg-icons'import { fadt } from '@fortawesome/duotone-thin-svg-icons'import { fasds } from '@fortawesome/sharp-duotone-solid-svg-icons'import { faTwitter, faFontAwesome } from '@fortawesome/free-brands-svg-icons'import { faHatCowboy } from '@fortawesome/pro-thin-svg-icons'import { faHatChef } from '@fortawesome/sharp-solid-svg-icons'
library.add(fas, fass, fad, fadt, fasds, faTwitter, faFontAwesome, faHatCowboy, faHatChef)
In our call to library.add()
we’re passing:
fas
: which represents all of the icons in@fortawesome/free-solid-svg-icons
*fad
: represents all the Duotone Solid icons in@pro-duotone-svg-icons
*fadt
: represents all Duotone Thin icons in@duotone-thin-svg-icons
*fass
: represents all Sharp Solid icons in@sharp-solid-svg-icons
*fasds
: represents all Sharp Duotone Solid icons in@sharp-duotone-solid-svg-icons
*faThreads
,faFontAwesome
,faHatCowboy
, andfaHatChef
: Adding each of these icons individually allows us to use them without importing all the icons in the style
* For the pacakges above marked with a ”*”, the import adds all the icons in that package/style. Which makes it easy to add any of the icons in that package using the icon name as a string anywhere in the app. But it’s also a lot of icons.
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 might look something like this:
import React from 'react'import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
export const Mailroom = () => ( <div> <FontAwesomeIcon icon="fa-solid fa-dog" /> <FontAwesomeIcon icon="fa-sharp fa-solid fa-hippo" /> <FontAwesomeIcon icon="fa-duotone fa-solid fa-circle-user" /> <FontAwesomeIcon icon="fa-duotone fa-thin fa-fish" /> <FontAwesomeIcon icon="fa-sharp-duotone fa-solid fa-dolphin" /> </div>)
import React from 'react'import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
export const Showcase = () => ( <div> <FontAwesomeIcon icon="fa-brands fa-twitter" /> <FontAwesomeIcon icon="fa-brands fa-font-awesome" /> <FontAwesomeIcon icon="fa-thin fa-hat-cowboy" /> <FontAwesomeIcon icon="fa-sharp fa-solid fa-hat-chef" /> </div>)
You’ll notice we were able use the imported brand icons without explicitly importing them in the component. And we used the dog
, hippo
, feather
, fish
, and dolphin
icons without explicitly importing them anywhere. But, each 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.
Troubleshooting
Section titled “Troubleshooting”Missing Icons?
Section titled “Missing Icons?”If some of your icons aren’t showing up, make sure you have included the icons - in the style you’re trying to use - in your subset for your Kit (if you’re using the Kit package), or imported the style package for the style of icon you’re trying to use (if you’re using the SVG packages by style).
Importing Hyphenated Icon Names
Section titled “Importing Hyphenated Icon Names”If you’re having trouble importing icons with hyphenated names (like user-circle, thumbs-up, or face-smile), make sure you’re using the correct import format.
When importing from the Font Awesome package, prepend fa
and convert the icon name to PascalCase (also known as UpperCamelCase). For example:
Icon Name | Import Name |
---|---|
circle-user | faCircleUser |
thumbs-up | faThumbsUp |
face-smile | faFaceSmile |
Still Using Dynamic Importing?
Section titled “Still Using Dynamic Importing?”Dynamic Importing is no longer supported. If you’ve used this method for adding icons in the past, you’ll need to switch to one of the support methods noted above.