Create an react-images-viewer component.
npm install react-images-viewer

A react library that view photos list easily, and a simple, responsive viewer component for displaying an array of images.
``bash`recommended
yarn add react-images-viewer
or
`bash`
npm install react-images-viewer --save
`jsx
import React from "react";
import ImgsViewer from "react-images-viewer";
export default class Demo extends React.Component {
render() {
return (
{ src: "http://example.com/img1.jpg" },
{ src: "http://example.com/img2.png" },
]}
currImg={this.state.currImg}
isOpen={this.state.viewerIsOpen}
onClickPrev={this.gotoPrevious}
onClickNext={this.gotoNext}
onClose={this.closeViewer}
/>
);
}
}
`
Live Demo: guonanci.github.io/react-images-viewer
`bash`
yarn install
yarn
yarn start
Then open localhost:8000 in a browser.
Example using srcSet:
`jsx`
...
/>
`js`
const IMG_SET = [
{
src: "http://example.com/img1.svg",
caption: "A forest",
// As an array
srcSet: [
"http://example.com/img1_1024.jpg 1024w",
"http://example.com/img1_800.jpg 800w",
"http://example.com/img1_500.jpg 500w",
"http://example.com/img1_320.jpg 320w",
],
},
{
src: "http://example.com/img2.svg",
// As a string
srcSet:
"http://example.com/img2_1024.jpg 1024w, http://example.com/img2_800.jpg 800w, http://example.com/img2_500.jpg 500w, http://example.com/img2_320.jpg 320w",
},
];
| Property | Type | Default | Description |
| :------------------ | :--------- | :--------------- | :------------------------------------------------------------------------------------------------------------------------------ |
| backdropCloseable | boolean | false | Allow users to exit the viewer by clicking the backdrop |str
| closeBtnTitle | | undefined | Customize close esc title |boolean
| enableKeyboardInput | | true | Supports keyboard input - numberspace, esc, arrow left, arrow up, and arrow right, arrow down |
| currImg | | 0 | Required if you want to navigate the imgsViewer, The index of the image to display initially |undefined
| customControls | arr | | An array of elements to display as custom controls on the top of viewer |undefined
| imgs | arr | | Required. Array of image objects, See img opts table below |str
| imgCountSeparator | | ' / ' | Customize separator in the image count |boolean
| isOpen | | false | Required if you want to navigate the imgsViewer, Whether or not the viewer is displayed |str
| leftArrowTitle | | undefined | Customize of left arrow title |function
| onClickPrev | | undefined | Required if you want to navigate the imgsViewer, and fired on request of the previous image |function
| onClickNext | | undefined | Required if you want to navigate the imgsViewer, and fired on request of the next image |function
| onClose | | undefined | Required if you want to close the imgsViewer, and handle closing of the viewer |function
| onClickImg | | undefined | Handle click on current image |function
| onClickThumbnail | | undefined | Handle click on thumbnail |boolean
| preloadNextImg | | true | Whether to preload the next available image |str
| rightArrowTitle | | undefined | Customize right arrow title |boolean
| showCloseBtn | | true | Optionally display a close 'X' button in top right corner |boolean
| showImgCount | | true | Optionally display image index, e.g., "2 of 20" |number
| width | | 1024 | Maximum width of the carousel; defaults to 1024px |boolean
| spinnerDisabled | | false | Disable Spinner |function
| spinner | | DefaultSpinner | Spinner component class |str
| spinnerColor | | '#fff' | Color of spinner |number
| spinnerSize | | 50 | Size of spinner |boolean
| preventAutoScroll | | true | Determines whether auto-scrolling is prevented |
| Property | Type | Default | Description |
| :------- | :-------- | :---------- | :---------- | -------- |
| src | str | undefined | Required |string[] | string
| srcSet | | undefined | Optional |str
| caption | | undefined | Optional |str
| alt | | undefined` | Optional |