Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font-Awesome icon does not show in my ReactJS website

I'm trying to use a font next to my website's header in a react project I`m working on but it does not show.

I have imported the font-awesome package via yarn and imported the css of it in my index.js file but it does not show in my header.

index.js:

import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import "../node_modules/bootstrap/dist/css/bootstrap.min.css";
import "../node_modules/font-awesome/css/font-awesome.min.css";

ReactDOM.render(<App />, document.getElementById("root"));

Header Component:

import React, { Component } from "react";

export default class Header extends Component {
  render() {
    return (
      <div>
        <div className="col-md-6 m-auto">
          <h1 className="text-center mb-3">
            <i className="fas fa-book-open"> </i>
            English Dictionary
          </h1>
        </div>
      </div>
    );
  }
}

Only the text of the header is shown. I checked the browser for any errors and it did not show any, also when i inspect the page i see the element.

<i className="fas fa-book-open"> </i>

I hope any of you can help me, thanks!

like image 534
nadav atar Avatar asked May 24 '19 15:05

nadav atar


People also ask

Why are my font awesome icons not showing?

If you installed the Free Font Awesome version but try adding Pro icons to your web pages, they won't show. The solution to this is either you use the alternative free icons or upgrade to a Pro subscription/license.

Why is my react-icons not working?

To solve the error "Module not found: Error: Can't resolve 'react-icons'", make sure to install the react-icons package by opening your terminal in your project's root directory and running the command npm install react-icons and restart your dev server.

Can I use font awesome in react?

Once you've installed all the packages you need for your project, there are two ways you can use Font Awesome 5 with React.


1 Answers

You will need to follow these steps to use font-awesome with react:

Install these dependencies:

npm i @fortawesome/fontawesome-svg-core
npm i @fortawesome/free-solid-svg-icons
npm i @fortawesome/react-fontawesome

Make necessary imports where you need to use them:

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faTimes } from '@fortawesome/free-solid-svg-icons'

Use in JSX:

<FontAwesomeIcon icon={faTimes} />
like image 121
Anurag Srivastava Avatar answered Oct 02 '22 12:10

Anurag Srivastava