0

I'm learning React JS and I'm trying to import font-awesome with npm modules to display this icon:

import React, { Component } from "react"; class Like extends Component { render() { return <i class="far fa-heart"></i>; } } export default Like; 

How the "import" statement at the top of the file should look like for this icon? I tried to read official documentation but can't really understand the pattern...

1 Answer 1

4

If the required modules are installed

$ 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/fontawesome-free-regular 

you can use them like

import React, { Component } from "react"; import { FontAwesomeIcon} from '@fortawesome/react-fontawesome'; import { faHeart } from '@fortawesome/fontawesome-free-regular'; class Like extends Component { render() { return <FontAwesomeIcon icon={faHeart}/>; } } export default Like; 

For more details refer to the official github repo

In case you're a pro member, you can install other modules as well, the listed once are free for use.

To have both types available, you can do import alias

import React, { Component } from "react"; import { FontAwesomeIcon} from '@fortawesome/react-fontawesome'; import { faHeart as regularHeart } from '@fortawesome/fontawesome-free-regular'; import { faHeart as solidHeart } from '@fortawesome/free-solid-svg-icons'; class Like extends Component { render() { return {this.props.solid ? <FontAwesomeIcon icon={solidHeart}/> : <FontAwesomeIcon icon={regularHeart}/>}; } } export default Like; 
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks a lot, it worked! Do I understand correctly that every time the name of needed icon contains "-" we need to use camel case typing like so = "fa-heart -> faHeart" ?
Correct, also note that the regular (non solid) icons are in a separate module @fortawesome/fontawesome-free-regular
Got it! And also, how would I add second class "far" to "faHeart"? Because currently it displays a solid icon and "far" should make it appear with some space inside.
That's what I meant, use @fortawesome/fontawesome-free-regular instead
fontawesome.com/icons/heart?style=regular - there is a regular version(which I need) and a solid version. How can I access the regular one?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.