React Image Gallery
react-image-gallery is a feature-rich, responsive image gallery component designed for React applications. Currently stable at version 2.1.2, it receives regular patch updates, indicating active maintenance. Key capabilities include native mobile swipe gestures, customizable thumbnails with various positioning options, browser or CSS-based fullscreen modes, comprehensive keyboard navigation, and built-in RTL support. It distinguishes itself by offering extensive customization via CSS custom properties and the ability to render custom content like videos or iframes within slides. The library aims to provide a robust and flexible solution for displaying image carousels, supporting modern React versions from 16 through 19 via peer dependencies, and ships with TypeScript types for an enhanced developer experience.
Common errors
-
Module not found: Can't resolve 'react-image-gallery/styles/image-gallery.css'
cause The CSS stylesheet for the gallery component is missing its import statement, or the path is incorrect.fixAdd `import 'react-image-gallery/styles/image-gallery.css';` to your application's entry point or the component where `ImageGallery` is used. This is mandatory since `v2.1.0`. -
TypeError: Cannot read properties of null (reading 'current')
cause Attempting to access `galleryRef.current` before the component has mounted or if the ref is not properly assigned to the `ImageGallery` component, or incorrect TypeScript typing.fixEnsure `galleryRef.current` is checked for `null` before access (e.g., `galleryRef.current?.slideToIndex(0)`). Also, verify the ref is passed to `<ImageGallery ref={galleryRef} />` and that `ImageGalleryRef` type is correctly used with `useRef`. -
TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
cause Image `original` or `thumbnail` properties within `items` array are `undefined` or not correctly typed as `string` when they are expected to be.fixEnsure all `original`, `thumbnail`, and other URL-related properties in your `GalleryItem` array are always strings and not `undefined`. For optional properties, provide a default empty string or handle `undefined` values explicitly.
Warnings
- breaking Starting with `v2.1.0`, the library no longer automatically injects its CSS stylesheet into the DOM. Developers must explicitly import `react-image-gallery/styles/image-gallery.css` in their application. Failure to do so will result in an unstyled or poorly styled gallery. This change improves SSR compatibility and reduces bundle size.
- gotcha When programmatically interacting with the gallery (e.g., using `slideToIndex`, `play`, `pause`), it's essential to correctly type the `useRef` hook with `ImageGalleryRef` for accurate TypeScript inference and to avoid potential runtime errors or type mismatches.
- gotcha The `items` prop expects an array of objects, where each object *must* include an `original` URL for the main image and typically a `thumbnail` URL for the thumbnail. Incorrectly structured `items` arrays can lead to rendering issues, missing images, or unexpected behavior.
- gotcha Ensure that image paths provided in the `items` prop are correctly resolved by your build system. Relative paths for local images might require specific handling (e.g., `require()` for static assets or moving them to the `public` folder) depending on your React project setup (e.g., Create React App, Vite).
Install
-
npm install react-image-gallery -
yarn add react-image-gallery -
pnpm add react-image-gallery
Imports
- ImageGallery
const ImageGallery = require('react-image-gallery');import ImageGallery from 'react-image-gallery';
- GalleryItem
import { GalleryItem } from 'react-image-gallery';import type { GalleryItem } from 'react-image-gallery'; - ImageGalleryRef
import { ImageGalleryRef } from 'react-image-gallery';import type { ImageGalleryRef } from 'react-image-gallery'; - CSS Stylesheet
import 'react-image-gallery/styles/image-gallery.css';
Quickstart
import { useRef } from "react";
import ImageGallery from "react-image-gallery";
import "react-image-gallery/styles/image-gallery.css"; // Essential for styling since v2.1.0
import type { GalleryItem, ImageGalleryRef } from "react-image-gallery";
const images: GalleryItem[] = [
{
original: "https://picsum.photos/id/1018/1000/600/",
thumbnail: "https://picsum.photos/id/1018/250/150/",
description: "A beautiful mountain landscape"
},
{
original: "https://picsum.photos/id/1015/1000/600/",
thumbnail: "https://picsum.photos/id/1015/250/150/",
description: "A calm lake with boats"
},
{
original: "https://picsum.photos/id/1019/1000/600/",
thumbnail: "https://picsum.photos/id/1019/250/150/",
description: "City skyline at sunset"
},
];
function MyGallery() {
const galleryRef = useRef<ImageGalleryRef>(null);
return (
<div style={{ maxWidth: '800px', margin: 'auto', padding: '20px' }}>
<h2>My Awesome Image Gallery</h2>
<ImageGallery
ref={galleryRef}
items={images}
onSlide={(index) => console.log("Slid to", index)}
showFullscreenButton={true}
showPlayButton={true}
lazyLoad={true}
thumbnailPosition="bottom"
/>
<button onClick={() => galleryRef.current?.slideToIndex(0)} style={{ marginTop: '15px' }}>
Go to First Slide
</button>
</div>
);
}
export default MyGallery;