Search code examples
reactjsfont-awesomefont-awesome-5react-font-awesome

How to use the FontAwesome Icon Library in React?


I have a React project that uses FontAwesome icons. To keep the code DRY, I wanted to keep my icon imports in a separate file using the Icon Library, per the guide here: https://fontawesome.com/how-to-use/javascript-api/setup/library.

I added a fontawesome.js file with the following code:

import { library } from "@fortawesome/fontawesome-svg-core";

import { faCode, faHighlighter } from "@fortawesome/free-solid-svg-icons";

library.add(faCode, faHighlighter);

and imported fontawesome.js into my index.js. Then I tried rendering faCode to my Icon.js component:

import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";

export default function Icon() {
  return (
    <div>
        <FontAwesomeIcon icon={['fas', 'fa-code']} />
    </div>
  );
}

only to encounter the following error:

Could not find icon {prefix: "fas", iconName: "fa-code"} 

I think I'm missing an import here. The documentation wasn't entirely clear on what to do after the Library has been set up. Any thoughts?


Solution

  • Here is another way to cache font awesome in react. find it out from this LINK