React.js ライブラリ「react-hook-qrcode」を使ってQRコードを生成する
- 2020.05.27
- React
- react-hook-qrcode, React.js, ライブラリ

ライブラリ「react-hook-qrcode」をインストールすると、QRコードを生成することが可能です。ここでは、react.jsでreact-hook-qrcodeを利用するための手順と簡単な使い方を記述してます。
環境
- OS CentOS Linux release 8.0.1905 (Core)
- node V12.13.1
- npm 6.14.2
- React 16.13.0
react.js環境構築
下記のコマンドで構築してます。ここでは、react-appという名前でプロジェクトを作成してます。
1 |
create-react-app react-app |
react-hook-qrcodeインストール
作成したプロジェクトに移動して、インストールします。
ここではbootstrapも利用します。
1 2 3 4 5 |
## 作成したプロジェクトに移動 cd react-app ## インストール npm install react-hook-qrcode |
react-hook-qrcode使い方
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 |
import React from 'react'; import { useQRCode } from 'react-hook-qrcode'; const Sample = () => { const [inputRef] = useQRCode({ text: 'https://mebee.info/', options: { level: 'M', margin: 7, scale: 1, width: 200, color: { dark: '#3FB811', light: '#DDDDDD', }, }, }); return ( <div> <canvas ref={inputRef} /> </div> ); } 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にアクセスすると、QRコードを生成されてます。

-
前の記事
jenkins起動時にエラー「java.lang.UnsupportedClassVersionError: 58.0」が発生した場合の対処法 2020.05.26
-
次の記事
SPFレコードを設定する 2020.05.27
コメントを書く