react-resize-detector vs react-sizeme vs react-resize-aware
React Resize Detection Libraries Comparison
3 Years
react-resize-detectorreact-sizemereact-resize-awareSimilar Packages:
What's React Resize Detection Libraries?

React resize detection libraries provide mechanisms to monitor changes in the size of components or the window. These libraries are essential for responsive design, allowing developers to adapt layouts and styles based on the dimensions of elements. They help improve user experience by ensuring that components behave correctly when the viewport or their parent containers change size. Each library offers unique features and design philosophies, catering to different use cases in React applications.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
react-resize-detector1,151,282
1,28966.4 kB52 months agoMIT
react-sizeme650,935
1,958-344 years agoMIT
react-resize-aware22,131
56932 kB1a year agoMIT
Feature Comparison: react-resize-detector vs react-sizeme vs react-resize-aware

Detection Method

  • react-resize-detector:

    react-resize-detector employs a combination of ResizeObserver and window resize events to provide accurate size detection. This dual approach ensures that both component and window size changes are captured, offering more comprehensive coverage.

  • react-sizeme:

    react-sizeme utilizes a higher-order component (HOC) that wraps around your components to provide size information as props. This approach abstracts the complexity of size detection and allows for easy integration.

  • react-resize-aware:

    react-resize-aware uses a simple observer pattern to detect size changes. It listens for resize events and triggers updates when the size of the component changes, making it efficient for basic use cases.

Performance

  • react-resize-detector:

    With its use of ResizeObserver, react-resize-detector is efficient in handling multiple resize events without causing performance bottlenecks. It intelligently batches updates to minimize re-renders, making it suitable for complex applications.

  • react-sizeme:

    react-sizeme is designed to be performant, but the HOC pattern may introduce some overhead compared to more direct implementations. However, it balances ease of use with performance, making it a good choice for many applications.

  • react-resize-aware:

    This library is lightweight and optimized for performance, making it suitable for applications where minimal overhead is crucial. It avoids unnecessary re-renders by only updating when size changes occur.

Ease of Use

  • react-resize-detector:

    react-resize-detector offers a more complex API but provides extensive features for handling resizing. It may require a bit more understanding to leverage its full potential, but it is well-documented.

  • react-sizeme:

    react-sizeme is user-friendly due to its HOC approach, allowing developers to easily wrap components and access size props. It simplifies the process of integrating size detection into existing components.

  • react-resize-aware:

    react-resize-aware is straightforward to implement, requiring minimal setup. Its API is simple, making it easy for developers to quickly integrate resize awareness into their components.

Flexibility

  • react-resize-detector:

    react-resize-detector is highly flexible, allowing developers to customize how and when size changes are detected. It supports various configurations to suit different application needs.

  • react-sizeme:

    react-sizeme provides flexibility through its HOC pattern, enabling developers to easily compose size-aware components. However, it may not offer as many customization options as react-resize-detector.

  • react-resize-aware:

    This library is less flexible in terms of configuration options, focusing primarily on size awareness without additional features. It is best suited for straightforward scenarios.

Community and Support

  • react-resize-detector:

    react-resize-detector has a growing community and good documentation, making it easier to find support and examples for implementation.

  • react-sizeme:

    react-sizeme benefits from a solid community and a wealth of resources, including examples and tutorials, making it easier for developers to get help and share knowledge.

  • react-resize-aware:

    react-resize-aware has a smaller community and fewer resources available, which may pose challenges for troubleshooting and finding examples.

How to Choose: react-resize-detector vs react-sizeme vs react-resize-aware
  • react-resize-detector:

    Select react-resize-detector for a more comprehensive and feature-rich solution that supports both component and window resizing detection. It is suitable for complex layouts that require precise control over resizing events and performance optimizations.

  • react-sizeme:

    Opt for react-sizeme if you want a library that provides a higher-order component (HOC) approach to size detection, making it easy to integrate into existing components. It is beneficial for applications that require a declarative approach to handle size changes.

  • react-resize-aware:

    Choose react-resize-aware if you prefer a lightweight solution that focuses on awareness of size changes without additional dependencies. It is ideal for simple use cases where you need to trigger re-renders based on size changes.

README for react-resize-detector

Handle element resizes like it's 2025!

Live demo

Modern browsers now have native support for detecting element size changes through ResizeObservers. This library utilizes ResizeObservers to facilitate managing element size changes in React applications.

🐥 Tiny ~2kb

🐼 Written in TypeScript

🐠 Used by 170k repositories

🦄 Produces 100 million downloads annually

No window.resize listeners! No timeouts!

Should you use this library?

Consider CSS Container Queries first! They now work in all major browsers and might solve your use case with pure CSS.

CSS Container Queries Example
<div class="post">
  <div class="card">
    <h2>Card title</h2>
    <p>Card content</p>
  </div>
</div>
.post {
  container-type: inline-size;
}

/* Default heading styles for the card title */
.card h2 {
  font-size: 1em;
}

/* If the container is larger than 700px */
@container (min-width: 700px) {
  .card h2 {
    font-size: 2em;
  }
}

Use this library when you need:

  • JavaScript-based resize logic with full TypeScript support
  • Complex calculations based on dimensions
  • Integration with React state/effects
  • Programmatic control over resize behavior

Installation

npm install react-resize-detector
# OR
yarn add react-resize-detector
# OR
pnpm add react-resize-detector

Quick Start

Basic Usage

import { useResizeDetector } from 'react-resize-detector';

const CustomComponent = () => {
  const { width, height, ref } = useResizeDetector<HTMLDivElement>();
  return <div ref={ref}>{`${width}x${height}`}</div>;
};

With Resize Callback

import { useCallback } from 'react';
import { useResizeDetector, OnResizeCallback } from 'react-resize-detector';

const CustomComponent = () => {
  const onResize: OnResizeCallback = useCallback((payload) => {
    if (payload.width !== null && payload.height !== null) {
      console.log('Dimensions:', payload.width, payload.height);
    } else {
      console.log('Element unmounted');
    }
  }, []);

  const { width, height, ref } = useResizeDetector<HTMLDivElement>({
    onResize,
  });

  return <div ref={ref}>{`${width}x${height}`}</div>;
};

With External Ref (Advanced)

It's not advised to use this approach, as dynamically mounting and unmounting the observed element could lead to unexpected behavior.

import { useRef } from 'react';
import { useResizeDetector } from 'react-resize-detector';

const CustomComponent = () => {
  const targetRef = useRef<HTMLDivElement>(null);
  const { width, height } = useResizeDetector({ targetRef });
  return <div ref={targetRef}>{`${width}x${height}`}</div>;
};

API Reference

Hook Signature

useResizeDetector<T extends HTMLElement = HTMLElement>(
  props?: useResizeDetectorProps<T>
): UseResizeDetectorReturn<T>

Props

| Prop | Type | Description | Default | | ----------------- | ------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- | ----------- | | onResize | (payload: ResizePayload) => void | Callback invoked with resize information | undefined | | handleWidth | boolean | Trigger updates on width changes | true | | handleHeight | boolean | Trigger updates on height changes | true | | skipOnMount | boolean | Skip the first resize event when component mounts | false | | refreshMode | 'throttle' \| 'debounce' | Rate limiting strategy. See lodash docs | undefined | | refreshRate | number | Delay in milliseconds for rate limiting | 1000 | | refreshOptions | { leading?: boolean; trailing?: boolean } | Additional options for throttle/debounce | undefined | | observerOptions | ResizeObserverOptions | Options passed to resizeObserver.observe | undefined | | targetRef | MutableRefObject<T \| null> | External ref to observe (use with caution) | undefined |

Advanced Examples

Responsive Component

import { useResizeDetector } from 'react-resize-detector';

const ResponsiveCard = () => {
  const { width, ref } = useResizeDetector();

  const cardStyle = {
    padding: width > 600 ? '2rem' : '1rem',
    fontSize: width > 400 ? '1.2em' : '1em',
    flexDirection: width > 500 ? 'row' : 'column',
  };

  return (
    <div ref={ref} style={cardStyle}>
      <h2>Responsive Card</h2>
      <p>Width: {width}px</p>
    </div>
  );
};

Chart Resizing

import { useResizeDetector } from 'react-resize-detector';
import { useEffect, useRef } from 'react';

const Chart = () => {
  const chartRef = useRef(null);
  const { width, height, ref } = useResizeDetector({
    refreshMode: 'debounce',
    refreshRate: 100,
  });

  useEffect(() => {
    if (width && height && chartRef.current) {
      // Redraw chart with new dimensions
      redrawChart(chartRef.current, width, height);
    }
  }, [width, height]);

  return <canvas ref={ref} />;
};

Performance Optimization

import { useResizeDetector } from 'react-resize-detector';

const OptimizedComponent = () => {
  const { width, height, ref } = useResizeDetector({
    // Only track width changes
    handleHeight: false,
    // Debounce rapid changes
    refreshMode: 'debounce',
    refreshRate: 150,
    // Skip initial mount calculation
    skipOnMount: true,
    // Use border-box for more accurate measurements
    observerOptions: { box: 'border-box' },
  });

  return <div ref={ref}>Optimized: {width}px wide</div>;
};

Browser Support

  • ✅ Chrome 64+
  • ✅ Firefox 69+
  • ✅ Safari 13.1+
  • ✅ Edge 79+

For older browsers, consider using a ResizeObserver polyfill.

Testing

const { ResizeObserver } = window;

beforeEach(() => {
  delete window.ResizeObserver;
  // Mock ResizeObserver for tests
  window.ResizeObserver = jest.fn().mockImplementation(() => ({
    observe: jest.fn(),
    unobserve: jest.fn(),
    disconnect: jest.fn(),
  }));
});

afterEach(() => {
  window.ResizeObserver = ResizeObserver;
  jest.restoreAllMocks();
});

Performance Tips

  1. Use handleWidth/handleHeight: false if you only need one dimension
  2. Enable skipOnMount: true if you don't need initial measurements
  3. Use debounce or throttle for expensive resize handlers
  4. Specify observerOptions.box for consistent measurements

License

MIT

❤️ Support

Show us some love and STAR ⭐ the project if you find it useful