1
votes

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?

2
If you replace <FontAwesomeIcon icon={['fas', 'fa-code']} /> by <FontAwesomeIcon icon={['fas', 'faCode']} /> ? - antoineso
Here is another way to cache font awesome in react. find it out from this LINK - qafoori
@antoineso didn't work for me :/ - mmz
@hrghafoori this worked for me! seems that I was crawling up the wrong documentation. Thanks for pointing me in the right direction! Feel free to post as an answer to mark as accepted. - mmz
try to put code instead of faCode. - antoineso

2 Answers

1
votes

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

0
votes

you need to import the solid (with 'fas' tag) icons from fontawesome:

npm i @fortawesome/free-solid-svg-icons or yarn add @fortawesome/free-solid-svg-icons