11

I am using FontawesomeIcon in a react js project and the names of the icons are coming from the database. I want to import the icons coming from the database from @fortawesome/free-solid-svg-icons dynamically

import React, { Component } from "react"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import {faImage} from "@fortawesome/free-solid-svg-icons"; export class Features extends Component { render() { return ( <div id="features" className="text-center"> <div className="container-fluid"> <div className="features-listing"> {this.props.data.map((item, index) => ( <div key={`${index}`}> {" "} <FontAwesomeIcon icon={item.icon} /> <h3>{item.title}</h3> </div> ))} </div> </div> </div> ); } } export default Features; 

5 Answers 5

15

There is actually a better way without having to iterate all icons (@Andrei Rosu's solution), you can export the whole brand and solid packages:

import { library } from '@fortawesome/fontawesome-svg-core' import { fab } from '@fortawesome/free-brands-svg-icons' import { fas } from '@fortawesome/free-solid-svg-icons' // This exports the whole icon packs for Brand and Solid. library.add(fab, fas) 

and to use them:

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' <FontAwesomeIcon icon={['fab', 'apple']} /> // any icon from Brand. <FontAwesomeIcon icon={['fas', 'coffee']} /> // any icon from Solid. 
Sign up to request clarification or add additional context in comments.

1 Comment

This could increase your bundle size considerably if only a small subset of the available icons are used
14

In App.js, you can import all the icons, as seen in this post, like this:

import {library} from '@fortawesome/fontawesome-svg-core'; import * as Icons from '@fortawesome/free-solid-svg-icons'; const iconList = Object.keys(Icons) .filter((key) => key !== 'fas' && key !== 'prefix') .map((icon) => Icons[icon]); library.add(...iconList); 

Then, inside components, only import the FontAwesomeIcon and use them:

import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'; <FontAwesomeIcon icon="times" /> 

5 Comments

Thank you for your answer, The icons names I want to use are unknown, they are coming from a request to the database, so this way will not work
@AmmarIsmaeel Please check my edited post on how you can import all those icons
Thx, It worked, though I did not like the idea of importing all icons, it seems its the only solution
@AmmarIsmaeel It would be more problematic to import them one by one dynamically, because the process is asynchronous. It shouldn't be a problem if you import them all at the beginning.
I believe there are +26k icons. It's doubtful your database is referencing all of them. So maybe query the unique list of icon name, and create a subset or a kit. This should dramatically scale back the icon load.
4

this can be done without importing all icons using react's dynamic imports. just replace the import string with whatever dynamic variable you need.

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import React from 'react' function example(){ const Icon = React.lazy(() => import('@fortawesome/free-solid-svg-icons/faEnvelope').then(({definition}) => ({default: () => <FontAwesomeIcon icon={definition} />}))); return <React.Suspense><Icon /></React.Suspense>; } 

Comments

1

I don't see any need to import all icons. You can import some icons in your app.js like:

import {library} from '@fortawesome/fontawesome-svg-core'; import { faAngleRight, faArrowRight ... } from '@fortawesome/pro-light-svg-icons'; library.add(faAngleRight, faArrowRight, ...) 

Then use it like:

<FontAwesomeIcon icon="fal fa-angle-right" /> 

or for dynamic use to any component (like button with icon) as prop:

<FontAwesomeIcon icon={icon} /> 

Comments

-1

Just simply add the Fontawesome CDN to your HTML Header and use any icon you want,

it would be something like this in react:

 <i className={object.ico}><i/> 

Object.ico should be a string containing the icon name like far fa-xyz

1 Comment

But I want to use the @fortawesome/react-fontawesome package, I do not want to use any cdn and place it in the html, Its better to have packages in your project

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.