React PhotoSwipe Gallery Component
react-photoswipe-gallery is a React component wrapper that integrates the PhotoSwipe JavaScript image gallery library into React applications. It simplifies the process of creating responsive image galleries by abstracting PhotoSwipe's imperative API into declarative React components like `Gallery` and `Item`. The current stable version is 4.0.0, which includes breaking changes focused on security and refactoring. The project maintains a moderate release cadence, with patch and minor updates appearing every few months, and major versions released less frequently. Its key differentiator lies in providing a native React experience for PhotoSwipe, managing the gallery's lifecycle and element references within the React component model, reducing boilerplate code, and ensuring compatibility with modern React features.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'current') OR TypeError: ref is not a function
cause Incorrectly assigning or failing to assign the `ref` object provided by the `Item` component's render prop to the actual DOM element, especially after the v3.0.0 `ref` handling changes.fixEnsure the `ref` prop passed down from the `Item` component's render function is correctly assigned to your `<img>` or `<a>` element, like `ref={ref as React.MutableRefObject<HTMLImageElement>}` (for TypeScript) or `ref={(el) => { if (ref) ref.current = el; }}` for a callback pattern. -
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
cause Attempting to render `Item` outside of `Gallery` or using an incorrect import statement (e.g., a default import instead of a named import for `Item`).fixVerify that `Item` components are always direct children of a `Gallery` component. Ensure `Item` is imported as a named export: `import { Item } from 'react-photoswipe-gallery'`. -
PhotoSwipe: Core has not been initialized. OR PhotoSwipe is not defined.
cause Missing the required PhotoSwipe CSS import, or `photoswipe` itself is not correctly installed or compatible with the `react-photoswipe-gallery` version.fixFirst, install `photoswipe` (`npm install photoswipe`) and ensure its version is compatible. Then, add `import 'photoswipe/dist/photoswipe.css';` to your application's entry point or relevant component file to include the necessary styles.
Warnings
- breaking As of v4.0.0, the `caption` prop for the `Item` component no longer supports raw HTML content. This change was implemented to mitigate potential Cross-Site Scripting (XSS) vulnerabilities.
- breaking Version 3.0.0 introduced significant changes to how `ref` props are handled for `Item` components. The `ref` type transitioned from a ref object to a ref callback, and passing `ref` to the underlying DOM node became mandatory even for single items. Manual casting of `ref` is also no longer required.
- gotcha This library has a strict peer dependency on `photoswipe`. Incompatible versions between `react-photoswipe-gallery` and `photoswipe` can lead to runtime errors, unexpected behavior, or a completely non-functional gallery. Consult `react-photoswipe-gallery`'s `package.json` for the exact compatible `photoswipe` version range.
- gotcha Prior to v4.0.0, the `caption` prop in the `Item` component was vulnerable to Cross-Site Scripting (XSS) attacks if unsanitized user-generated HTML was passed directly. This was addressed by removing HTML support in v4.0.0.
Install
-
npm install react-photoswipe-gallery -
yarn add react-photoswipe-gallery -
pnpm add react-photoswipe-gallery
Imports
- Gallery
const { Gallery } = require('react-photoswipe-gallery')import { Gallery } from 'react-photoswipe-gallery' - Item
import Item from 'react-photoswipe-gallery'
import { Item } from 'react-photoswipe-gallery' - useGallery
import { UseGallery } from 'react-photoswipe-gallery'import { useGallery } from 'react-photoswipe-gallery'
Quickstart
import React, { useRef } from 'react';
import { Gallery, Item } from 'react-photoswipe-gallery';
import 'photoswipe/dist/photoswipe.css'; // Essential PhotoSwipe base styles
const MyPhotoGallery: React.FC = () => {
return (
<Gallery
options={{
bgOpacity: 0.8,
zoomAnimationDuration: 300
}}
>
<Item
original="https://picsum.photos/id/1018/1000/600/"
thumbnail="https://picsum.photos/id/1018/100/60/"
width="1000"
height="600"
caption="A serene landscape from Unsplash"
cropped
>
{({ ref, open }) => (
<img
ref={ref as React.MutableRefObject<HTMLImageElement>}
onClick={open}
src="https://picsum.photos/id/1018/100/60/"
alt="Landscape thumbnail"
style={{ cursor: 'pointer', margin: '5px', borderRadius: '4px' }}
/>
)}
</Item>
<Item
original="https://picsum.photos/id/1015/1000/600/"
thumbnail="https://picsum.photos/id/1015/100/60/"
width="1000"
height="600"
caption="Abstract patterns and colors"
cropped
>
{({ ref, open }) => (
<img
ref={ref as React.MutableRefObject<HTMLImageElement>}
onClick={open}
src="https://picsum.photos/id/1015/100/60/"
alt="Abstract thumbnail"
style={{ cursor: 'pointer', margin: '5px', borderRadius: '4px' }}
/>
)}
</Item>
</Gallery>
);
};
export default MyPhotoGallery;