React.js ライブラリ「react-simple-image-slider」を使って軽量のLightBoxを実装する

ライブラリ「react-simple-image-slider」をインストールすると、軽量のLightBoxを実装することが可能です。ここでは、react.jsで「react-simple-image-slider」を利用するための手順と簡単な使い方を記述してます。
環境
- OS Rocky Linux release 8.4 (Green Obsidian)
- node v14.17.3
- npm 7.19.1
- yarn 1.22.10
- React 17.0.2
react.js環境構築
下記のコマンドで構築してます。ここでは、react-appという名前でプロジェクトを作成してます。
yarn global add create-react-app
npx create-react-app react-app
react-simple-image-sliderインストール
作成したプロジェクトに移動して、インストールします。
## 作成したプロジェクトに移動
cd react-app
## インストール
yarn add react-simple-image-slider
react-simple-image-slider使い方
srcディレクトリにsample.jsと名前で、以下のコードを記述します。
import React from 'react'
import SimpleImageSlider from 'react-simple-image-slider'
const images = [
{ url: "https://mebee.info/wp-content/uploads/2021/05/image-29-890x500.png" },
{ url: "https://mebee.info/wp-content/uploads/2021/04/image-182-890x500.jpg" },
{ url: "https://mebee.info/wp-content/uploads/2020/12/image-100-890x500.png" },
]
const Sample = () => {
return (
<div>
<SimpleImageSlider
width={800}
height={500}
images={images}
showBullets={true}
showNavs={true}
/>
</div>
);
};
export default Sample;
次に、srcディレクトリ配下にあるApp.jsを下記のように編集します。
import React from 'react';
import Sample from './sample';
import './App.css';
function App() {
const style = {
width: "30%",
margin: "0 auto",
marginTop: 150,
};
return (
<div className="App">
<div style={style}>
<Sample />
</div>
</div>
);
}
export default App;
実行します。
yarn start
ブラウザから http://プライベートIP:3000にアクセスすると、LightBoxが表示されていることが確認できます。
-
前の記事
python クラスを継承する 2021.11.01
-
次の記事
GitKrakenでtagを追加する 2021.11.01
コメントを書く