score:62
react uses the classname
attribute, like the dom.
if you use the development build, and look at the console, there's a warning. you can see this on the jsfiddle.
warning: unknown dom property class. did you mean classname?
score:0
checkout react icons pretty dope and worked well.
score:1
in my case i was following the documentation for react-fontawesome
package, but they aren't clear about how to call the icon when setting the icons into the library
this is was what i was doing:
app.js file
import {facoffee} from "@fortawesome/pro-light-svg-icons";
library.add(fasearch, fafilesearch, facoffee);
component file
<fontawesomeicon icon={"coffee"} />
but i was getting this error
then i added the alias when passing the icon prop like:
<fontawesomeicon icon={["fal", "coffee"]} />
and it is working, you can find the prefix value in the icon.js file, in my case was: facoffee.js
score:1
if someone wants to do it via the official documentation. here is how i did and what i understood.
- install the library as mentioned on font awesome page using npm or yarn for example in general i used these
npm i --save @fortawesome/fontawesome-svg-core
npm install --save @fortawesome/free-solid-svg-icons
npm install --save @fortawesome/react-fontawesome
- you need two import library from '@fortawesome/fontawesome-svg-core' if you want to use in general or globally, it provides the access to the icons everywhere in your project.
import { library } from '@fortawesome/fontawesome-svg-core';
- to use the icons you need to import them based on the npm or yarn library you have installed. i would recommend importing this in app.js where it will be easily visible. like if i was using a free-tier shopping cart icon and it was solid you can import like below (if you are not sure if its solid, regular, etc or what library just click on the icon it will list out the different versions and based on the icon you like you can import the library for react).
import { fashoppingcart } from '@fortawesome/free-solid-svg-icons';
- for adding the icon name i would recommend writing the import statement and library and then precede with
fa
and start typing the name it will auto-recommend as soon as you type (checked in vscode) which will make it less complicated to find.
- you need to add the icons in the library for making it accessible globally in your project separated by a comma
library.add( fachecksquare, faambulance, fashoppingcart);
- after which you can import the following component
import { fontawesomeicon } from '@fortawesome/react-fontawesome';
and use it like below wherever you need the icon. for the name, it's the same as listed on the style class on the fontawesome site, for the icons. eg: shopping cart is listed as shopping-cart
<fontawesomeicon icon="shopping-cart" />
it should work now after this.
score:2
i was experienced this case; i need the react/redux site that should be working perfectly in production.
but there was a 'strict mode'; shouldn't lunch it with these commands.
yarn global add serve
serve -s build
should working with only click the build/index.html file. when i used fontawesome with npm font-awesome, it was working in development mode but not working in the 'strict mode'.
here is my solution:
public/css/font-awesome.min.css
public/fonts/font-awesome.eot
*** other different types of files(4) ***
*** i copied these files for node_module/font-awesome ***
*** after copied then can delete the font-awesome from package.json ***
in public/index.html
<link rel="stylesheet" href="%public_url%/css/font-awesome.min.css">
after all of above steps, the fontawesome works nicely!!!
score:4
personally, i found react-fontawesome to be unwieldy because of the way it requires each icon to be individually imported. if you would like to use font awesome version 5 with react without using react-fontawsome, you can do it as follows:
download the fontawesome free package from font awesome themselves here: https://fontawesome.com/how-to-use/on-the-web/setup/hosting-font-awesome-yourself
create a directory 'fontawesome' in the 'public' directory of your react app.
copy the 'css' and 'webfonts' directories from the downloaded zip into this folder.
add the following line to the 'head' tag of your 'index.html' file:
<link href="%public_url%/fontawesome/css/all.css" rel="stylesheet">
you can now use font awesome icons in the usual way:
<i classname="fas fa-globe-europe"></i>
score:5
as 'praveen m p' said you can install font-awesome as a package. if you have yarn you can run:
yarn add font-awesome
if you don't have yarn do as praveen said and do:
npm install --save font-awesome
this will add the package to your projects dependencies and install the package in your node_modules folder. in your app.js file add
import 'font-awesome/css/font-awesome.min.css'
score:5
alexander's answer from above really helped me out!
i was trying to get social accounts icons in the footer of my app i created with reactjs so that i could easily add a hover state to them while also having them link my social accounts. this is what i ended up having to do:
$ npm i --save @fortawesome/fontawesome-free-brands
then at the top of my footer component i included this:
import react from 'react';
import './styles/footer.css';
import fontawesomeicon from '@fortawesome/react-fontawesome';
import {fatwitter, falinkedin, fagithub} from '@fortawesome/fontawesome-free-brands';
my component then looked like this:
<a href='https://github.com/yourusernamehere'>
<fontawesomeicon classname ='font-awesome' icon={fagithub} />
</a>
worked like a charm.
score:6
you need to install the package first.
npm install --save react-fontawesome
or
npm i --save @fortawesome/react-fontawesome
don't forget to use classname
instead of class
.
later on you need to import them in the file where you wanna use them.
import 'font-awesome/css/font-awesome.min.css'
or
import fontawesomeicon from '@fortawesome/react-fontawesome'
score:6
after struggling with this for a while i came up with this procedure (based on font awesome's documentation here):
as stated, you'll have to install fontawesome, react-fontawesome and fontawesome icons library:
npm i --save @fortawesome/fontawesome-svg-core
npm i --save @fortawesome/free-solid-svg-icons
npm i --save @fortawesome/react-fontawesome
and then import everything to your react app:
import { library } from '@fortawesome/fontawesome-svg-core'
import { fontawesomeicon } from '@fortawesome/react-fontawesome'
import { fastroopwafel } from '@fortawesome/free-solid-svg-icons'
library.add(fastroopwafel)
here comes the tricky part:
to change or add icons, you'll have to find the available icons in your node modules library, i.e.
<your_project_path>\node_modules\@fortawesome\free-solid-svg-icons
each icon has two relevant files: .js and .d.ts, and the file name indicates the import phrase (pretty obvious...), so adding angry, gem and check-mark icons looks like this:
import { fastroopwafel, faangry, fagem, facheckcircle } from '@fortawesome/free-solid-svg-icons'
library.add(fastroopwafel, faangry, fagem, facheckcircle)
to use the icons in your react js code, use:
<fontawesomeicon icon=icon_name/>
the icon name can be found in the relevant icon's .js file:
e.g. for facheckcircle look inside facheckcircle.js for 'iconname' variable:
...
var iconname = 'check-circle';
...
and the react code should look like this:
<fontawesomeicon icon=check-circle/>
goodluck!
score:10
in case you are looking to include the font awesome library without having to do module imports and npm installs, put this in the head section of your react index.html page:
public/index.html (in head section)
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"/>
then in your component (such as app.js) just use standard font awesome class convention. just remember to use classname instead of class:
<button classname='btn'><i classname='fa fa-home'></i></button>
score:13
npm install --save font-awesome
import 'font-awesome/css/font-awesome.min.css';
then
<i classname="fa fa-shopping-cart" style={{fontsize:24}}></i>
<span classname="badge badge-danger" style={{position:"absolute", right:5, top:5}}>number of items in cart</span>
score:21
the simplest solution is:
install:
npm install --save @fortawesome/fontawesome-svg-core
npm install --save @fortawesome/free-solid-svg-icons
npm install --save @fortawesome/react-fontawesome
import:
import { fontawesomeicon } from '@fortawesome/react-fontawesome';
import { fathumbsup } from '@fortawesome/free-solid-svg-icons';
use:
<fontawesomeicon icon={ fathumbsup }/>
score:34
npm install --save-dev @fortawesome/fontawesome-free
in index.js
import '@fortawesome/fontawesome-free/css/all.min.css';
then use icons like below :
import react, { component } from "react";
class like extends component {
state = {};
render() {
return <i classname="fas fa-heart"></i>;
}
}
export default like;
score:37
you can also use the react-fontawesome
icon library. here's the link: react-fontawesome
from the npm page, just install via npm:
npm install --save react-fontawesome
require the module:
var fontawesome = require('react-fontawesome');
and finally, use the <fontawesome />
component and pass in attributes to specify icon and styling:
var mycomponent = react.createclass({
render: function () {
return (
<fontawesome
classname='super-crazy-colors'
name='rocket'
size='2x'
spin
style={{ textshadow: '0 1px 0 rgba(0, 0, 0, 0.1)' }}
/>
);
}
});
don't forget to add the font-awesome css to index.html:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">
score:45
https://github.com/fortawesome/react-fontawesome
install fontawesome & react-fontawesome
$ npm i --save @fortawesome/fontawesome
$ npm i --save @fortawesome/react-fontawesome
$ npm i --save @fortawesome/fontawesome-free-solid
$ npm i --save @fortawesome/fontawesome-free-regular
$ npm i --save @fortawesome/fontawesome-svg-core
then in your component
import react, { component } from 'react';
import { fontawesomeicon } from '@fortawesome/react-fontawesome'
import { fachecksquare, facoffee } from '@fortawesome/fontawesome-free-solid'
import './app.css';
class app extends component {
render() {
return (
<div classname="app">
<h1>
<fontawesomeicon icon={facoffee} />
</h1>
</div>
);
}
}
export default app;
score:371
if you are new to react js and using create-react-app cli command to create the application, then run the following npm command to include the latest version of font-awesome.
npm install --save font-awesome
import font-awesome to your index.js file. just add below line to your index.js file
import '../node_modules/font-awesome/css/font-awesome.min.css';
or
import 'font-awesome/css/font-awesome.min.css';
don't forget to use classname as attribute
render: function() {
return <div><i classname="fa fa-spinner fa-spin">no spinner but why</i></div>;
}
Source: stackoverflow.com
Related Query
- How to include a Font Awesome icon in React's render()
- How to put an onClick event on an font awesome icon in react?
- How to get Font Awesome Icon in react app for a pseudo element in css
- How to find Icon names for Font Awesome to import with React?
- How can I use Font Awesome icons in an Office UI Fabric React icon button component?
- How to eliminate gap between Font Awesome Icon and paragraph in React JS?
- how to have webpack include font awesome woff files in a react based chrome extension?
- How do I conditionally render font awesome icons in react
- How to remove underline from font awesome icon
- React cannot render font awesome icon
- How to simulate a click on a Font Awesome Icon using Jest?
- How to use a font awesome icon to react-table v7 with expand / contract?
- How to change the color of radio button with font awesome icon in react, styled components
- How to fetch font awesome icon from JSON data in reactjs?
- How to use Icons like Font Awesome in Gatsby
- react font awesome renders big icon until scales down
- Toggling Font Awesome 5 icon with React
- Can't import Font Awesome icon
- setState not updating font awesome icon
- Add link to Font Awesome icon in ReactJS
- Using a Font Awesome Icon with react-select v2?
- How to achieve Font Awesome stack icons feature in react native
- How can I get Font Awesome 5 to work with React?
- React.js not rendering correct font awesome icon
- How to add Font Awesome into React.js?
- Font Awesome icon in React
- How do I conditionally render an icon based on the value of a prop?
- Adding margin to font awesome icon in React
- Putting a font awesome svg icon inside an input field
- How can I render an icon dynamically?
More Query from same tag
- Value in global context is empty in a specific case, not empty anywhere else
- Reflux Stores - Avoiding regression issues/naming conflicts
- Testing specific MaterialUI Icon
- React-redux dispatch is undefined when rendered inside specific component
- How to hide Material-UI raised button when printing
- react-select with selectable group labels
- reactjs parent trigger child component
- How to change react-select color
- Play Framework File Upload Not Working (React/Scala)
- React Hook useEffect has a missing dependency: 'dispatch'
- nyc (istanbul) doesn't work when NODE_ENV is set to test
- react js, how to sequentially / synchronously update states that depend on each other?
- Mock a non-default function using Jest and React
- Open and close Dialog using Props on ReactJS? My modal keeps open and don't close
- How to get parameters on Reach Router
- How to insert variable in `validationSchema` Formik and Yup?
- VSCode does not format JSX Correctly
- How can I respond to the width of an auto-sized DOM element in React?
- Opening Same Modal Window from different Components and communicating exact parent
- Django, DRF (& React) Foreign key, how to display the name value instead of the foreign key id
- Redux Saga with socket.io stacked loops make data duplication on redux state
- React render Onsen UI Toolbar
- How to useStyles based on props inside a breakpoint with material-ui
- Api call not firing up in redux action with debouce
- React: question about using ref and useLayoutEffect to determine if a component is mounted or unmounted
- How to redirect to another page if prop is not defined in react
- Apollo useQuery() throws Error: ECONNREFUSED when running async test in React Testing Library
- How to replace space with in a string (ReactJS) and render in HTML?
- React pdf-renderer does not display characters č, ć and đ
- Post route for register