react-resize-detector vs react-resize-aware
React Resize Detection Libraries Comparison
1 Year
react-resize-detectorreact-resize-awareSimilar Packages:
What's React Resize Detection Libraries?

React resize detection libraries provide developers with the ability to monitor changes in the size of DOM elements, enabling responsive designs and dynamic layouts. These libraries simplify the process of detecting when an element's dimensions change, allowing for efficient updates to the UI based on these changes. By utilizing these packages, developers can create more adaptive and user-friendly interfaces that respond to varying screen sizes and content changes without the need for manual event handling or complex calculations.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
react-resize-detector1,207,5161,27363.3 kB02 months agoMIT
react-resize-aware24,92357032 kB0a year agoMIT
Feature Comparison: react-resize-detector vs react-resize-aware

Performance Optimization

  • react-resize-detector:

    react-resize-detector provides a more feature-rich approach to resize detection, which may introduce some overhead compared to react-resize-aware. However, it offers optimizations like throttling and debouncing to manage performance during rapid resize events, making it suitable for complex layouts.

  • react-resize-aware:

    react-resize-aware is designed to be lightweight and efficient, minimizing the number of re-renders triggered by resize events. It uses a subscription model that ensures components only re-render when necessary, thus improving overall performance in applications where resize detection is frequent.

API Simplicity

  • react-resize-detector:

    react-resize-detector offers a more complex API due to its additional features, which may require a steeper learning curve. However, this complexity allows for greater flexibility and customization, catering to advanced use cases.

  • react-resize-aware:

    The API of react-resize-aware is straightforward, allowing developers to easily integrate resize detection into their components with minimal configuration. This simplicity makes it accessible for developers who want quick implementation without extensive setup.

Element Support

  • react-resize-detector:

    react-resize-detector supports multiple elements and can detect changes in both window and element sizes, making it a better choice for applications that require monitoring of various components simultaneously.

  • react-resize-aware:

    react-resize-aware primarily focuses on detecting size changes for a single element, making it ideal for scenarios where only one component's size needs to be monitored.

Use Cases

  • react-resize-detector:

    Ideal for complex applications that need to manage multiple resize events across different components, such as dashboards or layouts that adjust dynamically based on content size.

  • react-resize-aware:

    Best suited for simple applications where you need to react to size changes of individual components without additional overhead. It's ideal for scenarios like responsive design adjustments or conditional rendering based on size.

Community and Maintenance

  • react-resize-detector:

    react-resize-detector has a larger community and more extensive documentation, which can be beneficial for developers seeking support and resources. Its broader feature set also means it may receive more frequent updates and improvements.

  • react-resize-aware:

    react-resize-aware has a smaller community compared to react-resize-detector, but it is actively maintained and provides essential functionality without unnecessary bloat.

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

    Choose react-resize-detector if you need a more comprehensive solution that offers additional features such as support for multiple elements and the ability to handle both window and element resizing. This package is ideal for applications that require more robust handling of resize events and may benefit from its more extensive capabilities.

  • react-resize-aware:

    Choose react-resize-aware if you prefer a lightweight solution that provides a simple API for detecting size changes of a component. It is particularly useful for applications where performance is a concern, as it minimizes re-renders by using a more efficient approach to resize detection.

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