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.
npm install react-image-galleryVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to import and use the ImageGallery component with a basic set of images, including TypeScript types for props and refs, and shows an example of programmatic control. It also includes the crucial CSS import for proper styling.
Add `import 'react-image-gallery/styles/image-gallery.css';` to your main application file or component where the gallery is used.
Use `const galleryRef = useRef<ImageGalleryRef>(null);` and ensure `ImageGalleryRef` is imported as a type: `import type { ImageGalleryRef } from 'react-image-gallery';`Verify that each object in the `items` array conforms to the `GalleryItem` interface, at minimum providing `original` and `thumbnail` string URLs.
For local images, consider placing them in the `public` folder and using absolute paths like `/images/my-image.jpg`, or importing them explicitly: `import image from './assets/image.jpg';` and using `image` as the source.
Add `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`.
Ensure `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`.Ensure 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.