React Measure

2.5.2 · maintenance · verified Sun Apr 19

react-measure is a React component that efficiently computes the dimensions and position of its child components. It leverages the browser's native `ResizeObserver` API to detect element dimension changes, offering a performant alternative to traditional polling or window resize event listeners. The library includes a polyfill for `ResizeObserver` to ensure broad browser compatibility across different environments. Currently stable at version 2.5.2, `react-measure` functions as a render prop component, allowing developers to precisely specify which rectangle properties (such as `client`, `offset`, `scroll`, `bounds`, or `margin`) are needed. Its key differentiator is the `ResizeObserver` integration, providing accurate and efficient measurement updates with minimal overhead, suitable for responsive designs and component queries. The package has been in a maintenance phase since its last major updates, providing a stable and reliable utility.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to wrap a component with `Measure` to obtain its dimensions and apply responsive styling based on its width. It logs the measurements to the console and displays them within the component itself.

import React, { Component } from 'react';
import Measure from 'react-measure';
import classNames from 'classnames';

class ItemToMeasure extends Component {
  state = {
    dimensions: {
      width: -1,
      height: -1,
    },
  };

  render() {
    const { width, height } = this.state.dimensions;
    // Example of a responsive class name based on width
    const className = classNames('measured-item', {
      'small-width-modifier': width < 400 && width !== -1,
      'medium-width-modifier': width >= 400 && width < 800,
      'large-width-modifier': width >= 800,
    });

    return (
      <Measure
        bounds // Request bounding client rect measurements
        margin // Request computed margin properties
        onResize={(contentRect) => {
          this.setState({
            dimensions: contentRect.bounds,
          });
          console.log('Component resized:', contentRect.bounds);
        }}
      >
        {({ measureRef, contentRect }) => (
          <div ref={measureRef} className={className} style={{ padding: '20px', border: '1px solid #ccc', minHeight: '100px' }}>
            <p>I am a component being measured!</p>
            {contentRect.bounds.width !== -1 && (
              <div>
                <p>Width: {Math.round(contentRect.bounds.width)}px</p>
                <p>Height: {Math.round(contentRect.bounds.height)}px</p>
              </div>
            )}
            <div style={{ background: '#eee', padding: '10px', marginTop: '10px' }}>
              Current Class: {className}
            </div>
          </div>
        )}
      </Measure>
    );
  }
}

// To run this, you'd typically render ItemToMeasure in a React root:
// ReactDOM.render(<ItemToMeasure />, document.getElementById('root'));

export default ItemToMeasure;

view raw JSON →