react-resize-detector vs react-sizeme vs react-resize-aware
React Resize Detection Libraries Comparison
1 Year
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,224,1091,26763.3 kB02 months agoMIT
react-sizeme786,0591,952-323 years agoMIT
react-resize-aware31,76757032 kB0a 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!

Is it necessary for you to use this library?

Container queries now work in all major browsers. It's very likely you can solve your task using pure CSS.

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;
  }
}

Installation

npm i react-resize-detector
// OR
yarn add react-resize-detector

Example

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

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

With props

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

const CustomComponent = () => {
  const onResize = useCallback(() => {
    // on resize logic
  }, []);

  const { width, height, ref } = useResizeDetector({
    handleHeight: false,
    refreshMode: 'debounce',
    refreshRate: 1000,
    onResize,
  });

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

With custom ref

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

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

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

API

| Prop | Type | Description | Default | | --------------- | ------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------- | | onResize | Func | Function that will be invoked with width, height and ResizeObserver entry arguments | undefined | | handleWidth | Bool | Trigger onResize on width change | true | | handleHeight | Bool | Trigger onResize on height change | true | | skipOnMount | Bool | Do not trigger onResize when a component mounts | false | | refreshMode | String | Possible values: throttle and debounce See lodash docs for more information. undefined - callback will be fired for every frame | undefined | | refreshRate | Number | Use this in conjunction with refreshMode. Important! It's a numeric prop so set it accordingly, e.g. refreshRate={500} | 1000 | | refreshOptions | Object | Use this in conjunction with refreshMode. An object in shape of { leading: bool, trailing: bool }. Please refer to lodash's docs for more info | undefined | | observerOptions | Object | These options will be used as a second parameter of resizeObserver.observe method. | undefined | | targetRef | Ref | Use this prop to pass a reference to the element you want to attach resize handlers to. It must be an instance of React.useRef or React.createRef functions | undefined |

Testing with Enzyme and Jest

Thanks to @Primajin for posting this snippet

const { ResizeObserver } = window;

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

  wrapper = mount(<MyComponent />);
});

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

it('should do my test', () => {
  // [...]
});

License

MIT

❤️

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