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']}/>
Related
I Install all 3 required modules in react app.
npm i --save #fortawesome/fontawesome-svg-core
npm install --save #fortawesome/free-solid-svg-icons
npm install --save #fortawesome/react-fontawesome
I am trying to add font awesome icon in my program. But it is not showing up.Do I need to copy Font Awesome link and add in my app. That's what I used to do it in my html program.
(For example )
But in react how to do it? I tried the way which is in the document but it is not working. I only added the part of my program.Please help.
import React from 'react';
import { Form, Button} from 'react-bootstrap';
import './ContactUs.css';
import { library } from '#fortawesome/fontawesome-svg-core'
import { fab } from '#fortawesome/free-brands-svg-icons'
import { faCheckSquare, faShare} from '#fortawesome/free-solid-svg-icons'
library.add(fab, faCheckSquare, faShare)
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
// import { faShare } from '#fortawesome/free-solid-svg-icons'
class ContactUs extends React.Component{
render(){
return(
<div>
<Button className="btn send-button mt-4">
SUBMIT
<FontAwesomeIcon icon="share" />
</Button>
</div>
)
}
}
export default ContactUs;
I don't find any issue except one thing.
As you are using individual icon instead of, <FontAwesomeIcon icon="share" /> you must use write like this
<FontAwesomeIcon icon={faShare} />
Please check the below link for reference.
https://codesandbox.io/s/frosty-glade-9ful6?file=/src/App.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"));
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";
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!
I'm attempting to using react-fontawesome and implementing it in what seems to me to be exactly the same as the readme: https://github.com/danawoodman/react-fontawesome/blob/master/readme.md
import React from 'react';
import FontAwesome from 'react-fontawesome'
...
export default class ComponentName extends React.Component {
render() {
return (
<div>
<div>
<span>
<FontAwesome
className='super-crazy-colors'
name='rocket'
size='2x'
spin
style={{ textShadow: '0 1px 0 rgba(0, 0, 0, 0.1)' }}
/>
SOME TEXT
</span>
</div>
...
</div>
)
}
}
But I'm not seeing an icon in the DOM. Although I do see in the Chrome Dev Tools:
<span style="text-shadow:0 1px 0 rgba(0, 0, 0, 0.1);" aria-hidden="true" class="fa fa-rocket fa-2x fa-spin super-crazy-colors" data-reactid=".0.$/=11.0.0.$/=10.$/=10.$/=10"></span>
which I feel like should be a <i> tag. I tried changing the <span>...</span> to an <i>...</i> in "edit as HTML" in the dev tools and the icon still didn't show.
I have add-module-exports in my plugins and stage-2 in my presets in my webpack.config.
Can anyone tell me if I'm missing something? Do I need some other package other than react-fontawesome to make this work? Do I need to import the standard font-awesome.css or load a font-awesome CDN? Any help would be greatly appreciated, thanks!
I had the same problem. Read their Readme.md, and you see that there is this note:
Note: This component does not include any of the Font Awesome CSS or fonts, so you'll need to make sure to include those on your end somehow, either by adding them to your build process or linking to CDN versions.
So the most simple way is to link to the fontawesome cdn in your html.
<head>
<meta charset="UTF-8">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-
awesome.min.css" rel="stylesheet" integrity="sha384-
wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN"
crossorigin="anonymous">
</head>
Run npm install font-awesome --save
In your index.js or App.js or YourComponent.js, import the minified CSS file.
import 'font-awesome/css/font-awesome.min.css';
As the other answers mention, you need to include the icons in your page somehow. Check out the react-fontawesome example here:
https://github.com/danawoodman/react-fontawesome/blob/master/examples/index.html
You can see that the developer has included the font-awesome CSS on line #5.
On a separate note, Font Awesome v5 has been released, and you should consider moving to it. Read relevant docs here:
https://www.npmjs.com/package/#fortawesome/react-fontawesome
To use v5:
Install dependencies:
$ npm i --save #fortawesome/fontawesome
$ npm i --save #fortawesome/react-fontawesome
$ npm i --save #fortawesome/fontawesome-free-solid
Your component can then use icons like so:
import ReactDOM from 'react-dom';
import FontAwesomeIcon from '#fortawesome/react-fontawesome'
import { faCoffee } from '#fortawesome/fontawesome-free-solid'
const element = (
<FontAwesomeIcon icon={faCoffee} />
)
ReactDOM.render(element, document.body);
You can also build a library of commonly used icons in your app, for easy reference. Check out working code here: https://codesandbox.io/s/8y251kv448
More details about the library function available here:
https://www.npmjs.com/package/#fortawesome/react-fontawesome#build-a-library-to-reference-icons-throughout-your-app-more-conveniently
The only example I got working that actually seems up-to-date and didn't throw "can't resolve 'react' errors:
https://scotch.io/tutorials/using-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"));
Font Awesome now has an official React component that’s available to use their icons in your React applications.
Step 1 :
you must install 3 important packages in your project using a package manager like npm:
npm i --save #fortawesome/fontawesome-svg-core
npm install --save #fortawesome/free-solid-svg-icons
npm install --save #fortawesome/react-fontawesome
Step 2 :
if you're planning on styling the icons you must download these two extra packages :
npm install --save #fortawesome/free-brands-svg-icons
npm install --save #fortawesome/free-regular-svg-icons
Step 3:
Once you’ve installed all the packages you need for your project, now you can use the icons.
Import this packages in your component like this:
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
After that you can import an icon into your component like this (example
using an icon with the class as "fas fa-atom")
import { faAtom } from '#fortawesome/free-solid-svg-icons'
P.S : you can import multiple icons in one import with adding a coma between
each one.
Step 4 :
use the imported icons
const element = <FontAwesomeIcon icon={faAtom} />
SOURCE : https://fontawesome.com/v5.15/how-to-use/on-the-web/using-with/react
This seems to work perfectly for React
Installation:
$ yarn add #fortawesome/fontawesome-svg-core #fortawesome/free-solid-svg-icons #fortawesome/react-fontawesome
Usage:
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)
Font Awesome 5 React
As #Alee pointed out Fontaweome fonts aren't included in the package. You will have to import it yourself.
Install npm font-awesome package. If you use npm run npm install font-awesome --save or if you use yarn then run yarn add font-awesome
Now you just have to import font-awesome.less under less directory by writing import 'font-awesome/less/font-awesome.less'
Now you should see your icons working :)
import fontawesome styles from the package itself without loading any external css:
import "#fortawesome/fontawesome-svg-core/styles.css"; // import Font Awesome CSS
import { config } from "#fortawesome/fontawesome-svg-core";
config.autoAddCss = false; // Tell Font Awesome to skip adding the CSS automatically since it's being imported above
See article to get more info
copy and paste in your html ...
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css">
NEXT JS - Import styles this way if you are using NEXT jS
import '../node_modules/#fortawesome/fontawesome-svg-core/styles.css'
I suspect that you have not yet imported the CSS that FontAwesome needs - download the minified CSS file from FontAwesome's Web site and import it into your app.scss file or wherever else you're importing other stylesheets.
The react-fontawesome library is helping you create elements with class names like 'up-arrow', but without the stylesheet, your project will not know that there are icons corresponding to those classes.
Better to install everything all at once and then only import the icon you want,
npm i --save #fortawesome/fontawesome-svg-core #fortawesome/free-solid-svg-icons #fortawesome/react-fontawesome #fortawesome/free-brands-svg-icons #fortawesome/free-regular-svg-icons
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome';
import { faSpinner } from '#fortawesome/free-solid-svg-icons';
make sure to import the icon from the exact pack, you can use ctrl + space to use the power of suggections to fiding icons and for changing pack when your importing icon.
Make sure that you have installed the Font Awesome package correctly
using a package manager like npm or yarn. You can install it by
running the following command in your project directory:
npm install --save #fortawesome/fontawesome-free
Check if you have imported the required font awesome files in your
project. Make sure to import the necessary stylesheets and the icons
library in your project files. You can do this by adding the
following code to your main index.js file:
import '#fortawesome/fontawesome-free/css/all.css';
import { library }from '#fortawesome/fontawesome-svg-core';
import { fab } from '#fortawesome/free-brands-svg-icons';
import { fas } from'#fortawesome/free-solid-svg-icons';
Ensure that the icon name and style properties are set correctly.
You can use the FontAwesomeIcon component from the
#fortawesome/react-fontawesome package to display icons in your
project. For example, you can display a faUser icon with a solid
style using the following code:
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome';
import { faUser } from '#fortawesome/free-solid-svg-icons';
<FontAwesomeIcon icon={faUser} style={{color: 'black'}} />
If the above steps do not work, try clearing your browser cache and
restarting your development server.
If the issue persists, you may want to check the Font Awesome
documentation and GitHub repository for additional help and support
.
Try adding
<script src='https://use.fontawesome.com/3bd7769ce1.js'></script>
to your main index.html within your react project.