react-resize-detector vs react-measure vs react-visibility-sensor vs react-dimensions
React Dimension and Visibility Measurement Libraries Comparison
1 Year
react-resize-detectorreact-measurereact-visibility-sensorreact-dimensionsSimilar Packages:
What's React Dimension and Visibility Measurement Libraries?

These libraries provide tools for measuring dimensions and visibility of React components, enabling responsive design and dynamic layouts. They help developers detect changes in size or visibility of elements, which is crucial for creating adaptive UIs that respond to user interactions and changes in viewport size. Each library offers unique features and approaches to handle these measurements, catering to different use cases in web development.

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-measure375,5591,944-304 years agoMIT
react-visibility-sensor199,3662,326-836 years agoMIT
react-dimensions23,026450-297 years agoMIT
Feature Comparison: react-resize-detector vs react-measure vs react-visibility-sensor vs react-dimensions

Measurement Methodology

  • react-resize-detector:

    react-resize-detector uses a simple API to detect size changes by attaching event listeners to the window resize events. It is lightweight and does not require complex setup, making it easy to integrate into existing projects.

  • react-measure:

    react-measure employs a render prop pattern, allowing you to define how the component should render based on its dimensions. This flexibility enables you to customize the rendering logic based on the measurements, making it suitable for dynamic layouts.

  • react-visibility-sensor:

    react-visibility-sensor monitors the visibility of components using the Intersection Observer API, providing an efficient way to determine if a component is in the viewport. This is particularly useful for performance optimization in large applications.

  • react-dimensions:

    react-dimensions uses a higher-order component (HOC) to wrap your component, providing it with width and height props. This approach is straightforward and integrates seamlessly into your existing component structure, making it easy to use without extensive setup.

Performance

  • react-resize-detector:

    This library is highly performant due to its lightweight nature. It efficiently listens to resize events and updates only when necessary, making it suitable for performance-sensitive applications.

  • react-measure:

    Performance can vary based on how frequently the measurements are updated. It is designed to minimize re-renders by only updating when necessary, but developers should be cautious with complex layouts that may trigger frequent updates.

  • react-visibility-sensor:

    Performance is optimized through the use of the Intersection Observer API, which is efficient and reduces the need for continuous polling. This makes it a great choice for applications that require visibility tracking.

  • react-dimensions:

    Performance is generally good, but since it relies on HOC, it may introduce additional re-renders if not managed properly. It is essential to optimize the wrapped components to avoid unnecessary updates.

Ease of Use

  • react-resize-detector:

    This library is very user-friendly, with a simple API that allows developers to quickly add resize detection to their components without much overhead or complexity.

  • react-measure:

    react-measure may require a bit more understanding of render props, but it offers greater flexibility. Developers should be comfortable with this pattern to fully leverage the library's capabilities.

  • react-visibility-sensor:

    react-visibility-sensor is also easy to use, with a clear API that allows developers to quickly implement visibility detection. It abstracts away the complexities of the Intersection Observer API.

  • react-dimensions:

    react-dimensions is easy to use, especially for developers familiar with HOCs. The API is straightforward, allowing quick integration into existing components without a steep learning curve.

Use Cases

  • react-resize-detector:

    Perfect for scenarios where you need a lightweight solution to detect resizing events, such as responsive navigation menus or elements that need to adapt to the viewport size without heavy dependencies.

  • react-measure:

    Ideal for complex layouts where dimensions need to be tracked and rendered conditionally, such as dashboards or analytics applications that require real-time updates based on size changes.

  • react-visibility-sensor:

    Best used for lazy loading images or triggering animations when elements enter the viewport, making it suitable for performance optimization in content-heavy applications.

  • react-dimensions:

    Best suited for applications where you need to know the dimensions of a component for layout adjustments, such as responsive designs or dynamic content that changes size based on user interactions.

Community and Support

  • react-resize-detector:

    This library has a small but active community. It is straightforward, and the documentation is sufficient for most use cases, making it a solid choice for quick implementations.

  • react-measure:

    react-measure has a growing community and provides good documentation. It is actively maintained, making it a reliable choice for developers looking for a flexible measurement solution.

  • react-visibility-sensor:

    react-visibility-sensor has a strong community and is widely used in various projects. It has comprehensive documentation and examples, making it a well-supported choice for visibility detection.

  • react-dimensions:

    This library has a moderate community and support, with documentation available but may lack extensive examples or use cases. It is suitable for simple projects where community support is not a primary concern.

How to Choose: react-resize-detector vs react-measure vs react-visibility-sensor vs react-dimensions
  • react-resize-detector:

    Select react-resize-detector for a lightweight and straightforward solution that focuses on detecting size changes without the need for additional dependencies. It is perfect for cases where you need to respond to resizing events in a minimalistic way.

  • react-measure:

    Opt for react-measure if you require a more flexible and customizable approach to measuring dimensions. It allows you to track changes in size and provides a render prop pattern for better control over rendering based on measurements.

  • react-visibility-sensor:

    Use react-visibility-sensor when you need to detect the visibility of components within the viewport. This is particularly useful for lazy loading images or triggering animations when elements come into view.

  • react-dimensions:

    Choose react-dimensions if you need a simple solution for measuring component dimensions and want to leverage the power of higher-order components. It is ideal for scenarios where you need to access width and height of a component during its lifecycle.

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