React.js ライブラリ「react-icons」を使用して様々なアイコンを利用する
- 2020.03.25
- React
- react-icons, React.js, ライブラリ

ライブラリ「react-icons」をインストールすると、 Font awesomeやMaterial、Typiconsなどのアイコンが簡単に利用可能です。ここでは、react.jsでreact-iconsを利用するための手順と簡単な使い方を記述してます。
環境
- OS CentOS Linux release 8.0.1905 (Core)
- node V12.13.1
- npm 6.14.1
- React 16.13.0
react.js環境構築
下記のコマンドで構築してます。ここでは、react-appという名前でプロジェクトを作成してます。
1 |
create-react-app react-app |
react-iconsインストール
作成したプロジェクトに移動して、インストールします。
1 2 3 4 5 |
## 作成したプロジェクトに移動 cd react-app ## インストール npm install react-icons |
react-icons使い方
srcディレクトリにsample.jsと名前で下記のコードを記述します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import React, { Component } from 'react'; import { FaApple, FaAndroid } from 'react-icons/fa'; import { IoIosCloudCircle, IoIosHeartEmpty } from "react-icons/io"; import { MdDone } from "react-icons/md"; import { TiChartLineOutline } from "react-icons/ti"; import { GoBeaker } from "react-icons/go"; import { FiAlertOctagon } from "react-icons/fi"; import { GiFlyingTrout } from "react-icons/gi"; import { WiBarometer } from "react-icons/wi"; import { DiChrome } from "react-icons/di"; import { AiFillApi } from "react-icons/ai"; class Sample extends Component { render() { return ( <h3> <FaApple /> <FaAndroid /> <IoIosCloudCircle /> <IoIosHeartEmpty /> <MdDone /> <TiChartLineOutline /> <GoBeaker /> <FiAlertOctagon /> <GiFlyingTrout /> <WiBarometer /> <DiChrome /> <AiFillApi /> </h3> ) } } export default Sample |
次に、srcディレクトリ配下にあるApp.jsを下記のように編集します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import React from 'react'; import Sample from './sample'; import './App.css'; function App() { const style = { width: "50%", margin: "0 auto", marginTop: 150 }; return ( <div className="App"> <div style={style}> <Sample /> </div> </div> ); } export default App; |
実行します。
1 |
npm start |
ブラウザから http://プライベートIP:3000にアクセスすると、 Font awesomeやMaterial、Typiconsなどのアイコンが実装されていることが確認できます。

-
前の記事
Vue.js lingalleryを利用してシンプルなギャラリーを実装する 2020.03.25
-
次の記事
Ruby bunderインストール時にエラー「ERROR: Could not find a valid gem ‘bunder’ (>= 0) in any repository」が発生した場合の対処法 2020.03.25
なぜreact-springインストールしてるんですか?
誤字です。大変申し訳ないです。修正致しました。ご指摘ありがとうございました