react-cropper provides a React component wrapper for the popular Cropper.js library, enabling declarative image cropping within React applications. As of version 2.3.3, it offers robust integration with React's component lifecycle and state management, abstracting away the direct manipulation of the Cropper.js instance. It is actively maintained with frequent patch releases, focusing on bug fixes and dependency updates, ensuring compatibility and stability. Key differentiators include its direct reliance on the well-established Cropper.js, providing a familiar API for those already accustomed to the underlying library, and its comprehensive TypeScript type definitions for improved developer experience. While it wraps Cropper.js, users still need to understand the core Cropper.js options and methods, which are exposed via a ref to the component.
npm install react-cropperVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to import, render, and interact with the Cropper component using a `useRef` hook to access the underlying Cropper.js instance for image data and display a preview.
Ensure you have `import 'cropperjs/dist/cropper.css';` in your entry file or a component where the Cropper is used.
Use `useRef<ReactCropperElement>(null)` in functional components and access methods via `cropperRef.current?.cropper.methodName()`.
Review props being passed to the `Cropper` component. If you need to pass specific HTML `img` attributes, ensure they are compatible or use a workaround if the prop filtering causes issues.
Carefully configure `initialAspectRatio`, `viewMode`, `minCropBoxHeight`, `minCropBoxWidth`, and `responsive` options. Refer to the Cropper.js documentation for details on each option's effect.
Always check if `cropperRef.current?.cropper` exists before calling any methods on it. This often happens if an event fires before the image is loaded and the cropper instance is ready.
Add `import 'cropperjs/dist/cropper.css';` to your application's entry point or the component file where `Cropper` is used.
Ensure your ref is typed as `useRef<ReactCropperElement>(null)` and always null-check `cropperRef.current` before accessing `cropperRef.current.cropper`.
Run `npm install react-cropper` or `yarn add react-cropper` to install the package. Verify the package is listed in `node_modules` and your `package.json`.