Font Awesome Pro Icons not working in React - reactjs

I have font awesome pro installed in my project but the pro icons are not showing up in my project. The free version works fine.
import React from 'react';
import ProfileCard from './ProfileCard';
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome';
import { faCalendar } from '#fortawesome/free-solid-svg-icons';
const MeetingStart = () => {
return (
<div className="mt-2 p-4">
<h4>Group Name</h4>
<div className="row d-flex justify-content-between">
<h2>Welcome</h2>
<div className="meeting-date row mr-2">
<FontAwesomeIcon icon={faCalendar} />
<FontAwesomeIcon icon="fa-regular fa-memo" />
<p>{new Date().toDateString().slice(0, 10)}</p>
</div>
</div>
</div>
);
};
export default MeetingStart;
In this example faCalendar works but fa-memo doesn't. I tried re-installing the packages but that didn't seem to change anything.
Link to docs:
https://fontawesome.com/docs/web/use-with/react/

Have you already checked to have all the dependencies installed for the pro version?
FONTAWESOME_NPM_AUTH_TOKEN=FONT-AWESOME-PACKAGE-TOKEN npm install --save #fortawesome/fontawesome-pro
npm i --save #fortawesome/pro-solid-svg-icons
npm i --save #fortawesome/pro-regular-svg-icons
npm i --save #fortawesome/pro-light-svg-icons
npm i --save #fortawesome/pro-thin-svg-icons
npm i --save #fortawesome/pro-duotone-svg-icons
npm i --save #fortawesome/react-fontawesome#latest

Related

Can't get font awesome to render

Trying to use font awesome and having trouble getting the icons to show up. I installed the following:
npm i --save #fortawesome/fontawesome-svg-core
npm i --save #fortawesome/free-solid-svg-icons
npm i --save #fortawesome/free-regular-svg-icons
npm i --save #fortawesome/react-fontawesome#latest
I picked an icon and put it on the page
<FontAwesomeIcon icon="fa-solid fa-circle-plus" />
This is what I'm importing:
import React from "react";
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
Do I need to import each icon separately? Am I missing something?

Module not found: Error: Can't resolve '#mui/icons-material/SearchOutlined'

I have installed both the packages already the icon and the core but still m getting this error, why??
import React from 'react'
import './Nav.css';
import SearchOutlinedIcon from '#mui/icons-material/SearchOutlined';
function Nav() {
return (
<div className = 'header' >
<div className="header_left">
<img className = 'header_image' src='https://cdn-icons-png.flaticon.com/512/174/174857.png' alt='' />
<div className="header_search">
{/* Search icon */}
<SearchOutlinedIcon/>
<input type = 'text' placeholder='search' />
</div>
</div>
</div>
)
}
export default Nav
Try to install also these packages.
// with npm
npm install #mui/material #emotion/react #emotion/styled
// with yarn
yarn add #mui/material #emotion/react #emotion/styled
I resolved this by changing
from
import SearchOutlinedIcon from '#mui/icons-material/SearchOutlined';
to
import { SearchOutlinedIcon } from "#mui/icons-material";
I had an exactly same problem to import icon from "#mui/icons-material/LightModeOutlinedIcon" but it worked like this (below)
import { LightModeOutlinedIcon } from "#mui/icons-material"
or
import LightModeOutlinedIcon from '#mui/icons-material/LightModeOutlined';
Lots of details missing in the question but here's my take on it:
I'm using npm v7.24.1 at the moment and I have the same dependency listed in package.json
"#mui/icons-material": "5.2.0"
And it worked fine when I used it in a component like this
import { SearchOutlined } from '#mui/icons-material';
Reflecting on the comment under your question, which I cannot reply to unfortunately, are you sure you are running npm install in the same directory your package.json is located in? Because unless your npm version is less than 5, npm install #mui/icons-material must affect package.json (and package-lock.json). Use --save flag with npm install command if the version of your npm is less than 5. Verify npm version with "npm -v".
Another thing you could do is check whether #mui/icons-material exists inside node_modules folder. If not, try adding #mui/icons-material to the dependencies section of package.json manually and then run npm install in the same directory.

How to reflect font awesome icons in the react project?

I have added the CDN link to the index.html of my React project.
You can check that in this repo
Added this line on line 16:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"
integrity="sha512-YWzhKL2whUzgiheMoBFwW8CKV4qpHQAEuvilg9FAn5VJUDwKZZxkJNuGM4XkWuk94WCrrwslk8yWNGmY1EduTA=="
crossorigin="anonymous" referrerpolicy="no-referrer" />
And added the i tags to my Components.
Line 14,15 lines in this file
<Nav className="mr-auto">
<Nav.Link href="/cart"><i className="fas fa-shopping-cart"></i>Cart</Nav.Link>
<Nav.Link href="/login"><i className="fas fa-user"></i>Login</Nav.Link>
</Nav>
But, the page is not showing the icons.
Please suggest, how to fix this?
The repo link: github
To get started you’ll need to install the following packages into your project using a package manager like npm and yarn.
https://fontawesome.com/v5.15/how-to-use/on-the-web/using-with/react
npm i --save #fortawesome/fontawesome-svg-core
npm install --save #fortawesome/free-solid-svg-icons
npm install --save #fortawesome/react-fontawesome
And Simply use Like this
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
import { faCoffee } from '#fortawesome/free-solid-svg-icons'
const element = <FontAwesomeIcon icon={faCoffee} />

How to return text + icon from a function in React

I have written a function in Reactjs and I want to return text with an icon if a certain condition meets. Here is how I wrote it
if (actionNeeded) {
return abbreviation + <i className={'fa fa-exclamation-circle fa-fw'} />;
}
This returns text [object Object] instead of text (icon). How do I get it to work?
You need to return a JSX element to have your <i> rendered.
In you current case, it will see that abbreviation is a string and will concatenate your <i>as such, giving you the result of an Object.toString().
You can use React.Fragment to do so:
return (
<React.Fragment>
{abbreviation} <i className="fa fa-exclamation-circle fa-fw" />
</React.Fragment>
);
You can use Font Awesome's npm packages for React. The benefit of doing it this way is that only the icons that you actually use will be added to the build.
Installation
npm i --save \
#fortawesome/fontawesome-svg-core \
#fortawesome/free-solid-svg-icons \
#fortawesome/react-fontawesome
main.js
import {faExclamationCircle, faFw} from '#fortawesome/free-solid-svg-icons';
import {library} from '#fortawesome/fontawesome-svg-core';
library.add(faExclamationCircle, faFw);
render(<App />, document.getElementById('root'));
component.js
import {FontAwesomeIcon} from '#fortawesome/react-fontawesome';
...
if (actionNeeded) {
return (
<>
{abbreviation} <FontAwesomeIcon icon="exclamation-circle" />
</>
);
}
I know I am late to answer this.
But something that worked for me:
Installation
$ npm i --save #fortawesome/fontawesome-svg-core
$ npm i --save #fortawesome/free-solid-svg-icons
$ npm i --save #fortawesome/react-fontawesome
Import in INdex.js
import '#fortawesome/fontawesome-free/css/all.min.css';
component.js
return (
<div className="errorDiv">
{showError &&
<React.Fragment>
<FontAwesomeIcon icon={faTimes} />
<span> </span>
{error}
</React.Fragment>
}
</div>
);

Font Awesome 5 use social/brand icons in React

The Font Awesome documentation shows only how to add regular/solid fonts to React. How about social icons?
First I grabbed the packages:
npm i --save #fortawesome/fontawesome-svg-core \
npm i --save #fortawesome/free-brands-svg-icons \
npm i --save #fortawesome/react-fontawesome
Note: I replaced npm i --save #fortawesome/free-solid-svg-icons \ with npm i --save #fortawesome/free-brands-svg-icons \
Then in React:
import { library } from '#fortawesome/fontawesome-svg-core'
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
import { faFacebookF } from '#fortawesome/free-brands-svg-icons'
library.add(faFacebookF);
And tried using a component:
<FontAwesomeIcon icon="fa-facebook-f" />
even tried:
and keep getting in the console
Could not find icon {prefix: "fas", iconName:
"fa-facebook-f"}
I believe I somehow have to get the fab prefix for brands.
This is the icon I want to use https://fontawesome.com/icons/facebook-f?style=brands
import { FontAwesomeIcon } from "#fortawesome/react-fontawesome"
import { faFacebook } from "#fortawesome/free-brands-svg-icons"
const icon = <FontAwesomeIcon icon={faFacebook} />
I found the spelling/casing of the brand icons on FontAwesome's GitHub
Try:
<FontAwesomeIcon icon={['fab', 'facebook-f']} />
Note that font awesome now has different icon sets. The solid set (fas) is the default. The facebook icon is in the brands set (fab).
Note that you must run the commands that you ran first:
npm i --save #fortawesome/fontawesome-svg-core
npm i --save #fortawesome/free-brands-svg-icons
npm i --save #fortawesome/react-fontawesome
I'd tried to import without installing first - and of course that didn't work.
Firstly
npm i --save #fortawesome/fontawesome-svg-core
npm i --save #fortawesome/free-brands-svg-icons
npm i --save #fortawesome/react-fontawesome
Then import in your project
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome';
import { faFacebookF , } from '#fortawesome/free-brands-svg-icons';
Use this
<FontAwesomeIcon icon={faFacebookF} />
Install these dependencies first
npm i --save #fortawesome/free-brands-svg-icons
npm i --save #fortawesome/fontawesome-svg-core
npm i --save #fortawesome/react-fontawesome
Create a custom library initFontAwesome.js and paste this code.
import { library } from "#fortawesome/fontawesome-svg-core";
import {fab, faTwitterSquare, faFacebook, faLinkedin, faGithub} from "#fortawesome/free-brands-svg-icons";
function initFontAwesome() {
library.add(fab, faTwitterSquare, faFacebook, faLinkedin, faGithub);
}
export default initFontAwesome;
In the App.js include the following code
import initFontAwesome from "./initFontAwesome";
initFontAwesome();
function App() {
return (
{/*---------Some code----------*/}
);
}
export default App;
In Home.jsx include the following code
import React from 'react';
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome';
{/*-------some Code---------*/}
<FontAwesomeIcon icon={['fab', 'twitter']} />
<FontAwesomeIcon icon={['fab', 'facebook']} />
<FontAwesomeIcon icon={['fab', 'linkedin']} />
<FontAwesomeIcon icon={['fab', 'github']} />
{/*-------Some Code---------*/}
This worked for me. I hope this helps!

Resources