Font Awesome 5 use social/brand icons in React - reactjs

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!

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?

Font Awesome Pro Icons not working in React

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

Using font-awesome in React JS

I'm new to React JS and I'm trying to add font-awesome into my project. I have installed Node.js and npm. I have also included the following packages:
$ npm i --save #fortawesome/fontawesome-svg-core
$ npm i --save #fortawesome/free-solid-svg-icons
$ npm i --save #fortawesome/react-fontawesome
Now, what should I do in index.js to be able to access all fonts and icons from these packages? I have checked multiple sources and information differs from site to site. Could you please explain how it's done and what should be written after "import" at the top of the file.
Also you can import all free font awesome icons with fas and fab prefixes in this way.
At first you need to install these packages.
npm i -S #fortawesome/fontawesome-svg-core #fortawesome/free-brands-svg-icons #fortawesome/free-solid-svg-icons #fortawesome/react-fontawesome
And then add fas and fab prefixes from FA lib to your root file
import { library } from "#fortawesome/fontawesome-svg-core";
import { fas } from "#fortawesome/free-solid-svg-icons";
import { fab } from "#fortawesome/free-brands-svg-icons";
import { FontAwesomeIcon } from "#fortawesome/react-fontawesome";
library.add(fas, fab);
const App = () => {
return (
<div>
<!-- Icon of fas prefix -->
<FontAwesomeIcon icon="home" />
<!-- Icon of fab prefix -->
<FontAwesomeIcon icon={['fab', 'google']} />
</div>
);
};
export default App;
First, make sure your package.json has font-awesome. If it does
not use npm i font-awesome to install it.
Second, You need to import the fonts that are in css folder of
font-awesome. Add the line to your index.js file.
import "../node_modules/font-awesome/css/font-awesome.min.css";
That information is inside the docs, here is the extract for imports
import ReactDOM from 'react-dom'
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
import { faCoffee } from '#fortawesome/free-solid-svg-icons'
const element = <FontAwesomeIcon icon={faCoffee} />
ReactDOM.render(element, document.body)
https://github.com/FortAwesome/react-fontawesome#explicit-import
Just stop the node application, install babel-loader finally run npm start. That should solve your problem.
Great Source to the point.
https://www.digitalocean.com/community/tutorials/how-to-use-font-awesome-5-with-react
import React from "react";
import { render } from "react-dom";
// get our fontawesome imports
import { faHome } from "#fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "#fortawesome/react-fontawesome";
// create our App
const App = () => (
<div>
<FontAwesomeIcon icon={faHome} />
</div>
);
// render to #root
render(<App />, document.getElementById("root"));

React App, Using Font Awesome brand icons in React VS Vue

I am trying to use the brand icons from Font Awesome, I have already used it before in a vue application, it's a little different from using the regular svg icons as the brands library is separate from the regular solid svg icons. this is how i used in a vue app.
// imports in the main.js file
import { library } from '#fortawesome/fontawesome-svg-core';
import { faCoffee } from '#fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '#fortawesome/vue-fontawesome';
import { faFacebookF } from '#fortawesome/free-brands-svg-icons';
library.add(faCoffee, faFacebookF);
Vue.component('fa-icon', FontAwesomeIcon)
// usage in a vue component - regular svg icons
<fa-icon icon="coffee" />
// brand icon
<fa-icon class="ic right" :icon="['fab', 'facebook-f']" />
Now i am trying to do a similar thing in a react app, But I am a little stuck on where and how to import and use it, In Vue, I defined the <fa-icon /> globally in my main.js, but things are a little different with React. here is my attempt in a react component.
// imports in my app.js file
import React from 'react';
import { library } from '#fortawesome/fontawesome-svg-core';
import { faCoffee } from '#fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome';
import { faFacebookF } from '#fortawesome/free-brands-svg-icons';
library.add(faCoffee, faFacebookF)
//this works
<FontAwesomeIcon icon="coffee" />
//this does not
<FontAwesomeIcon icon={"['fab', 'facebook-f']"} />
thanks in advance.
<FontAwesomeIcon icon={['fab', 'facebook-f']} />
https://www.npmjs.com/package/#fortawesome/react-fontawesome#build-a-library-to-reference-icons-throughout-your-app-more-conveniently
Install All Free Font Awesome Icons-- Using cmd on your project location:
npm i --save #fortawesome/fontawesome-svg-core
npm i --save #fortawesome/free-solid-svg-icons
npm i --save #fortawesome/react-fontawesome
npm i --save #fortawesome/free-brands-svg-icons
npm i --save #fortawesome/free-regular-svg-icons
Import this for Font Awesome:
import {FontAwesomeIcon} from "#fortawesome/react-fontawesome";
Add Font Awesome to your JSX:
<FontAwesomeIcon icon={faFacebookF}></FontAwesomeIcon>
<FontAwesomeIcon icon={faTwitter}/>
<FontAwesomeIcon icon={faGoogle}/>
And Lastly, import your brands-icon from your installed repository:
import {faFacebookF, faGoogle, faTwitter} from "#fortawesome/free-brands-svg-icons";

Cound not find icon react-fontawesome

I have a project on React and I need to integrate FontAwesome to it. I found official library and installed it as instructed in readme
$ npm i --save #fortawesome/fontawesome
$ npm i --save #fortawesome/react-fontawesome
When I try to use it in my code like this:
<FontAwesomeIcon icon='plus'/>
<FontAwesomeIcon icon={['fa', 'coffee']}/>
I am getting this error on the console:
Could not find icon {prefix: "fas", iconName: "plus"}
Could not find icon {prefix: "fa", iconName: "coffee"}
I tried to include FontAwesome css link to head but it didn't help. I am using npm v4.6.1; node v8.9.1; react v16.2.
You need to add any icons you wish to use, to a "library" for easy reference.
import React from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import fontawesome from '#fortawesome/fontawesome'
import FontAwesomeIcon from '#fortawesome/react-fontawesome';
import { faCheckSquare, faCoffee } from '#fortawesome/fontawesome-free-solid'
const styles = {
fontFamily: 'sans-serif',
textAlign: 'center',
};
fontawesome.library.add(faCheckSquare, faCoffee);
const App = () => (
<div style={styles}>
<FontAwesomeIcon icon="check-square" /><br /><br />
<FontAwesomeIcon icon="coffee" />
</div>
);
render(<App />, document.getElementById('root'));
Check out working code here:
https://codesandbox.io/s/8y251kv448
Also, read this:
https://www.npmjs.com/package/#fortawesome/react-fontawesome#build-a-library-to-reference-icons-throughout-your-app-more-conveniently
Just in case there are other idiots out there like me, make sure that you use the right name in referencing the icons.
I had
import { FontAwesomeIcon } from "#fortawesome/react-fontawesome";
import { library } from "#fortawesome/fontawesome-svg-core";
import { faUser } from "#fortawesome/free-solid-svg-icons";
library.add(faUser);
and
render() {
return (
<FontAwesomeIcon icon="faUser" />
);
}
when, in fact, the actual icon name is just "user", not "faUser":
render() {
return (
<FontAwesomeIcon icon="user" />
);
}
You can use FontAwesome icons without library like this:
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
import { faCoffee } from '#fortawesome/free-solid-svg-icons'
<FontAwesomeIcon icon={faCoffee} />
I've installed all the necessary packages as react-fontawesome says:
$ npm i --save #fortawesome/fontawesome-svg-core
$ npm i --save #fortawesome/free-solid-svg-icons
$ npm i --save #fortawesome/react-fontawesome
And if you find yourself not seeing your icon when trying to display faTrashAlt (or similarly named icon), not only do you have to remove the 'fa' when actually using your icon but also you have to convert the icon name from cameCase to "lisp-case".
So, after loading the alternative trash can icon this way:
fontawesome.library.add(faTrashAlt);
It's then used that way:
<FontAwesomeIcon icon="trash-alt" />
Just so you don't waste 20 precious minutes of your time.
Just because there is more than one way to get a fontawesome icon into a website, I'll give an example with react and fontawesome and explicit import. Explicit import is frugal. Only the exact icon from the fontawesome icon set is imported and the FontAwesomeIcon component icon attribute is set to the imported object instead of the name.
Example:
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome';
import { faCoffee } from '#fortawesome/free-solid-svg-icons';
import { faHistory } from '#fortawesome/pro-regular-svg-icons';
function exampleReactComponent() {
return (
<div>
<p><FontAwesomeIcon icon={faCoffee}/></p>
<p><FontAwesomeIcon icon={faHistory}/></p>
</div>
);
}
I don't disagree with setting the icon attribute of the FontAwesomeIcon by name it's just that I encountered console errors about finding icons by name and the resolution I discovered is to reference the object. Note the icon={} notation because it is different than using a quoted string of the icon name.
Explicit Import...
react-fontawesome npm package github link
Installing with npm...
installing fontawesome with npm link
To pass the icon path using an array like this
icon={['fa', 'coffee']}
You'll need to import all references of the library in a file and import this file in the index of your React app.
Example, create the file named fonts.js
import { library } from '#fortawesome/fontawesome-svg-core';
import { fas } from '#fortawesome/pro-solid-svg-icons';
import { far } from '#fortawesome/pro-regular-svg-icons';
import { fal } from '#fortawesome/pro-light-svg-icons';
import { fad } from '#fortawesome/pro-duotone-svg-icons';
import { fab } from '#fortawesome/free-brands-svg-icons';
library.add(fab);
library.add(fas);
library.add(far);
library.add(fal);
library.add(fad);
Now in the index.js of your app import the file that was created,
import '../configs/fonts.js'
This path is just an example, be sure that you are putting the right one.
For the free version of FontAwesome ;) #fortawesome
Install FontAwesome
npm install --save #fortawesome/react-fontawesome
npm install --save #fortawesome/fontawesome-free
npm install --save #fortawesome/fontawesome-svg-core
npm install --save #fortawesome/free-regular-svg-icons
npm install --save #fortawesome/free-solid-svg-icons
npm install --save #fortawesome/free-brands-svg-icons
Use it
...
import {FontAwesomeIcon} from '#fortawesome/react-fontawesome'
import '#fortawesome/fontawesome-free'
import {library} from '#fortawesome/fontawesome-svg-core'
import {far} from '#fortawesome/free-regular-svg-icons'
import {fas} from '#fortawesome/free-solid-svg-icons'
import {fab} from '#fortawesome/free-brands-svg-icons'
library.add(far,fas,fab);
...
<span className='fa fa-coffee'/>
<FontAwesomeIcon icon='coffee' />
<FontAwesomeIcon icon={['fas', 'coffee']}/>
<FontAwesomeIcon icon={['fab','react']}/>

Resources