react-resize-detector is a React library that leverages the native `ResizeObserver` API to detect element size changes within React applications. It provides a `useResizeDetector` hook for integrating resize observation directly into functional components. The current stable version is 12.3.0. Releases are somewhat frequent, with multiple major versions (v10, v11, v12) released within the last year, indicating active development and occasional breaking changes. Key differentiators include its small bundle size (~2kb), TypeScript support, and reliance on native `ResizeObserver` over `window.resize` listeners or timeouts, offering performant and accurate resize detection. The library is designed for scenarios requiring JavaScript-based resize logic, complex dimension-based calculations, or integration with React state/effects, differentiating itself from purely CSS-based container queries.
npm install react-resize-detectorVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic usage of `useResizeDetector` with an `onResize` callback to log dimensions, debounce updates, and attach the observer to a div element.
Update all `require()` statements to ES module `import` syntax. Ensure your project is configured for ESM (e.g., `"type": "module"` in package.json or using `.mjs` files).
Refactor `onResize: (width, height) => { ... }` to `onResize: ({ width, height, entry }) => { ... }`.Migrate any existing component-based usages to the `useResizeDetector` hook pattern. Refactor functional components to use the hook, or wrap class components if necessary.
Upgrade your React project to React 18 or 19. If unable to upgrade, you must stick to `react-resize-detector` versions prior to v10.0.0.
Prefer the default usage where the `ref` returned by `useResizeDetector` is directly applied to the element you wish to observe. Only use `targetRef` when you have a clear understanding of its implications.
Ensure your project is configured for ES modules (e.g., `"type": "module"` in `package.json`). Change `const { useResizeDetector } = require('react-resize-detector');` to `import { useResizeDetector } from 'react-resize-detector';`.Ensure `useResizeDetector` is only called at the top level of your React functional components or custom hooks. For class components, consider refactoring to functional components or using a wrapper.
Update your `onResize` callback signature from `(width, height) => { ... }` to `({ width, height, entry }) => { ... }`.