React.js ライブラリ「react-photoswipe-gallery」を使ってphotoswipeを利用する

ライブラリ「react-photoswipe-gallery」をインストールすると、photoswipeを利用することが可能です。ここでは、react.jsでreact-photoswipe-galleryを利用するための手順と簡単な使い方を記述してます。
環境
- OS CentOS Stream release 8
- node V14.15.1
- npm 6.14.8
- React 16.13.0
react.js環境構築
下記のコマンドで構築してます。ここでは、react-appという名前でプロジェクトを作成してます。
1 |
create-react-app react-app |
react-photoswipe-galleryインストール
作成したプロジェクトに移動して、インストールします。
1 2 3 4 5 |
## 作成したプロジェクトに移動 cd react-app ## インストール npm i react-photoswipe-gallery |
react-photoswipe-gallery使い方
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 35 36 37 38 39 |
import React from "react"; import 'photoswipe/dist/photoswipe.css' import 'photoswipe/dist/default-skin/default-skin.css' import { Gallery, Item } from 'react-photoswipe-gallery' const Sample = () => { return ( <div> <Gallery> <Item original="https://placekitten.com/1024/768?image=1" thumbnail="https://placekitten.com/80/60?image=1" width="1024" height="768" > {({ ref, open }) => ( <img ref={ref} onClick={open} src="https://placekitten.com/80/60?image=1" /> )} </Item> <Item original="https://placekitten.com/1024/768?image=2" thumbnail="https://placekitten.com/80/60?image=2" width="1024" height="768" > {({ ref, open }) => ( <img ref={ref} onClick={open} src="https://placekitten.com/80/60?image=2" /> )} </Item> </Gallery> </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にアクセスすると、photoswipeが作成されていることが確認できます。

-
前の記事
javascript 配列の重複データをコード1行で削除する 2021.01.23
-
次の記事
typescript interfaceのプロパティを読み取り専用にする 2021.01.23
コメントを書く