React Magnifier Component
raw JSON →react-magnifier is a lightweight React component designed to provide interactive image zooming functionality, allowing users to magnify specific areas of an image. It is currently stable at version 3.0.4, with recent releases focusing on bug fixes, TypeScript migration, and broad browser compatibility (targeting ES3 since v3.0.4). The library exhibits a moderate release cadence, addressing issues and introducing enhancements as needed. Key differentiators include built-in support for touch screens, the ability to specify a separate, higher-resolution image for the magnified view (e.g., a thumbnail and a full-size image), and extensive customization options for the magnifying glass's appearance, shape, and behavior through props such as `zoomFactor`, `mgShape`, and `mgShowOverflow`. It functions as a pure presentation component, requiring React v16.0.0 or higher as a peer dependency.
Common errors
error TypeError: Cannot read properties of undefined (reading 'defaultProps') ↓
react-magnifier to version 3.0.3 or higher. If upgrading is not immediately possible, explicitly pass a value (even an empty string) for zoomImgSrc or ensure your TypeScript configuration handles optional properties correctly. error Warning: Failed prop type: The prop `src` is marked as required in `Magnifier`, but its value is `undefined`. ↓
src prop of the Magnifier component. Example: <Magnifier src="/images/my-product.jpg" />. error Error: Invalid hook call. Hooks can only be called inside of the body of a function component. (If using older React versions) ↓
react and react-dom dependencies are at version 16.8.0 or higher. Also, check for potential issues with npm link or multiple installations of React that can cause the 'Invalid hook call' error. Warnings
breaking Version 3.0.0 migrated the codebase to TypeScript. While generally an improvement, this could introduce subtle breaking changes for consumers relying on previous internal module structures or type declarations if manually managed. It also fixed issues with event listener removal. ↓
breaking Version 2.0.0 introduced significant changes to the magnifying behavior, particularly close to the image borders. This may result in different glass positioning or cropping compared to prior versions. New props like `mgShowOverflow` and `borderWidth` were also added, potentially altering default visual characteristics. ↓
gotcha The `src` prop is required for the `Magnifier` component to render an image. Omitting it will lead to runtime errors or a non-functional component. ↓
gotcha The package lists `react: "^16.0.0"` as a peer dependency. Using `react-magnifier` with older versions of React may lead to peer dependency warnings or runtime issues, especially related to Hooks which were introduced in React 16.8. ↓
gotcha Between versions 3.0.0 and 3.0.2, TypeScript users might have encountered a type error related to the `zoomImgSrc` prop, specifically a missing default or incorrect type definition, leading to compilation failures. ↓
Install
npm install react-magnifier yarn add react-magnifier pnpm add react-magnifier Imports
- Magnifier wrong
import { Magnifier } from 'react-magnifier';correctimport Magnifier from 'react-magnifier'; - MagnifierProps
import type { MagnifierProps } from 'react-magnifier'; - Magnifier (CJS) wrong
const Magnifier = require('react-magnifier');correctconst Magnifier = require('react-magnifier').default;
Quickstart
import Magnifier from 'react-magnifier';
import yourImage from './path/to/your-high-res-image.jpg';
import yourThumbnail from './path/to/your-thumbnail.jpg';
export default function ImageZoomComponent() {
return (
<div style={{ width: '80%', margin: '20px auto' }}>
<h1>Product Image Viewer</h1>
<p>Hover or touch the image to magnify.</p>
<Magnifier
src={yourThumbnail}
zoomImgSrc={yourImage}
width={600}
height={400}
zoomFactor={2.5}
mgWidth={200}
mgHeight={200}
mgShape="square"
alt="Product showcase with zoom"
/>
<p>The `src` prop displays the initial image, and `zoomImgSrc` provides a higher-resolution image for the magnifying glass. If `zoomImgSrc` is omitted, `src` will be used for both.</p>
</div>
);
}