0

I'm trying to add a fontawesome icon in my react app using the code below (see the codesandbox) but the icons won't show.

./src/App.js

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

export default function App() {
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <FontAwesomeIcon icon="fa-solid fa-home"></FontAwesomeIcon>
      <div>hello</div>
      <FontAwesomeIcon icon="fas fa-edit" />
      <div>hello</div>
      <span>
        <i class="fas fa-edit"></i>
      </span>
    </div>
  );
}

./public/index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />
    <meta name="theme-color" content="#000000" />

    <script
      defer
      src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"
    ></script>
    <!--
      manifest.json provides metadata used when your web app is added to the
      homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
  </head>

  <body>
    <noscript>
      You need to enable JavaScript to run this app.
    </noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>

part of package.json

  "dependencies": {
    "@fortawesome/fontawesome-svg-core": "6.1.1",
    "@fortawesome/free-solid-svg-icons": "6.1.1",
    "@fortawesome/react-fontawesome": "0.1.18",
    "react": "18.0.0",
    "react-dom": "18.0.0",
    "react-scripts": "4.0.0"
  },
dapidmini
  • 1,490
  • 2
  • 23
  • 46
  • I just tried adding `import { faHome, faPencilSquare } from "@fortawesome/free-solid-svg-icons";` and then commenting it out and then suddenly it works.. weird... – dapidmini Jun 21 '22 at 19:27

1 Answers1

1

You might import the icon like:

import {faChevronDown} from '@fortawesome/free-solid-svg-icons'

<FontAwesomeIcon icon={faChevronDown} size={'xs'} />
Breno Prata
  • 712
  • 5
  • 10
  • somehow it works now even though all I did was adding an import and then commenting out that same line.. I wonder why... – dapidmini Jun 21 '22 at 19:29
  • This answer is very different to the code in your question - it is passing `faChevronDown` to `FontAwesomeIcon`, your code is passing a string. – GreenAsJade Jul 12 '22 at 22:36