LoginSignup
66
49

More than 1 year has passed since last update.

【React】Font Awesomeを導入するメモ

Last updated at Posted at 2020-02-05

React.jsのアプリケーションにFont Awesomeを導入する際にいつも忘れて一から調べているので備忘録を残します。
言語はTypeScriptを使ってますのでJavaScriptの方は適宜読み替えてください。

対象

  • Reactアプリ(not React Native)
  • Font Awesome無料版(有料版は使ったことないのでわからない)

パッケージ

インストール必須なもの

npm install @fortawesome/react-fontawesome
npm install @fortawesome/fontawesome-svg-core

使用するアイコンによってインストールするもの

npm install @fortawesome/free-brands-svg-icons
npm install @fortawesome/free-regular-svg-icons
npm install @fortawesome/free-solid-svg-icons

どのパッケージをインストールするかは、下の画像の赤枠で判断できます。
使いたいアイコンをFont Awesomeで調べてください。
twitterIcon.png

使い方

@fortawesome/react-fontawesomeからFontAwesomeIconimport
使いたいアイコンの定義を@fortawesome/free-brands-svg-iconsなどからimportして、FontAwesomeIconiconプロパティに渡します。

import React from 'react';
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faTwitter } from "@fortawesome/free-brands-svg-icons";
import { faCalendar } from "@fortawesome/free-regular-svg-icons";
import { faCoffee } from "@fortawesome/free-solid-svg-icons";

const App: React.FC = () => {
  const iconStyle: React.CSSProperties = { padding: 10, fontSize: 50 };

  return (
    <div style={{ textAlign: "center", padding: 50 }}>
      <FontAwesomeIcon style={iconStyle} icon={faTwitter} />
      <FontAwesomeIcon style={iconStyle} icon={faCalendar} />
      <FontAwesomeIcon style={iconStyle} icon={faCoffee} />
    </div>
  );
}

result.png

おしまい。

追記(2022-01-05)

react-icons を使うことをおすすめします。

66
49
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
66
49