简体   繁体   English

我如何在 React 中使用 Font Awesome 图标?

[英]How do I use Font awesome Icon in React?

I want to use fontAwesome icons in ReactJs.我想在 ReactJs 中使用 fontAwesome 图标。 I have not done this before.我以前没有这样做过。 I want to do something like home Icons as well as Signout Icons in React.我想在 React 中做一些像 home Icons 以及 Signout Icons 这样的事情。

Here is what I have done so far.这是我到目前为止所做的。

I included the font awesome Lib我包括字体真棒Lib

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'

Now I want to use in the application Like so现在我想在应用程序中使用像这样

                        <td width="150px">
                        <p align="center"/>
                        <button className="btn"><i className='icon-home'></i></button>
                        </td>
                        <td width="135">
                            <p align="center"/>
                        <button className="btnSignout"><i className='fa fa-home'></i></button>
                        </td>

How do I use FontAwesome in React Application?如何在 React 应用程序中使用 FontAwesome?

To use font awesome icons in easy way into your React project use react-icon library which not only provides support for font awesome but as well as provides for Bootstrap icons , Heroicons and more.要在 React 项目中以简单的方式使用 font awesome 图标,请使用 react-icon 库,它不仅提供对 font awesome 的支持,还提供 Bootstrap 图标、Heroicons 等。

Step 1. npm install react-icons --save步骤 1. npm install react-icons --save

step 2. Import a icon like this -步骤 2.导入这样的图标 -

 import { FaBeer } from 'react-icons/fa';
Note : FaBeer is icon name here for more icons visit to react-icons 注意:FaBeer 是这里的图标名称,更多图标请访问react-icons

Step 3. Use it something like this -第 3 步。像这样使用它 -

 class Question extends React.Component { render() { return <h3> Lets go for a <FaBeer />? </h3> } }

You can do this, check below code or click here for demo你可以这样做,检查下面的代码或点击这里进行 演示

Please double check the libraries I used and install请仔细检查我使用并安装的库

import React, { Component } from 'react';
import { render } from 'react-dom';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCoffee, faBars } from '@fortawesome/free-solid-svg-icons';
import 'bootstrap/dist/css/bootstrap.min.css';

class App extends Component {
  constructor() {
    super();
  }

  render() {
    return (
      <div>
        <p>Please check this</p>

        <button className="btn btn-primary">
          <FontAwesomeIcon icon={faBars} size={'1x'} />
          <span style={{ marginLeft: '10px' }}> My Button</span>
        </button>

        <br />
        <br />
        <FontAwesomeIcon icon={faCoffee} size={'2x'} />
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

To use Font Awesome in the react project, you should have to -要在 react 项目中使用Font Awesome ,您应该 -

  1. install the following packages (run the following commands in the terminal)安装以下软件包(在终端中运行以下命令)

npm i --save @fortawesome/fontawesome-svg-core
npm install --save @fortawesome/free-solid-svg-icons
npm install --save @fortawesome/react-fontawesome

  1. Now it's time to call and use into the JavaScript file.现在是调用和使用 JavaScript 文件的时候了。

import into the file the following 2 lines.将以下 2 行导入文件。

import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faMugHot } from '@fortawesome/free-solid-svg-icons';

The format of using icon is as follow,使用图标的格式如下,

<FontAwesomeIcon icon={faIconName, faAnotherIconName} />

for Example: `<FontAwesomeIcon icon={faAnchor, faMugHot} />例如:`<FontAwesomeIcon icon={faAnchor, faMugHot} />

Note: icon name will be camelcase fa<IconName> . Note:图标名称将为fa<IconName> for example, in the font-awesome website font name is fa-anchor we can write it as faAnchor .例如,在 font-awesome 网站中,字体名称为fa-anchor我们可以将其写为faAnchor for long name of the icon, if the name is fa-mug-hot we have to write it as faMugHot .对于图标的长名称,如果名称是fa-mug-hot我们必须将其写为faMugHot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM