react-measure, react-resize-aware, and react-sizeme are React utilities designed to track DOM element dimensions (width, height, position) and trigger re-renders when those dimensions change. They abstract the complexity of listening to resize events or using the ResizeObserver API directly. react-measure is the most feature-rich, offering both render props and hooks with support for various measurement types. react-sizeme is a classic Higher-Order Component (HOC) solution, now in maintenance mode, often found in legacy codebases. react-resize-aware provides a component-based approach focused on simplicity. All three solve the same core problem but differ significantly in API design, maintenance status, and underlying implementation strategies.
Building responsive React components often requires knowing the exact size of a DOM element. While CSS handles most layout needs, JavaScript measurements are necessary for dynamic behaviors like canvas rendering, custom tooltips, or complex grid calculations. react-measure, react-resize-aware, and react-sizeme all solve this problem, but they use different patterns and vary in long-term viability.
The biggest difference between these libraries is how they integrate into your component tree. This affects code readability and refactoring ease.
react-measure supports both Render Props and Hooks.
useMeasure) is clean and fits modern functional components.<Measure>) works well for older class components or specific render prop needs.// react-measure: Hook API
import { useMeasure } from 'react-measure';
function Box() {
const [ref, { width, height }] = useMeasure();
return <div ref={ref}>Width: {width}, Height: {height}</div>;
}
react-resize-aware uses a Component wrapper approach.
ResizeAware component.// react-resize-aware: Component Wrapper
import ResizeAware from 'react-resize-aware';
function Box() {
return (
<ResizeAware>
{({ width, height }) => (
<div>Width: {width}, Height: {height}</div>
)}
</ResizeAware>
);
}
react-sizeme relies on a Higher-Order Component (HOC).
withSize.size props into the wrapped component.// react-sizeme: HOC Pattern
import { withSize } from 'react-sizeme';
function Box({ size: { width, height } }) {
return <div>Width: {width}, Height: {height}</div>;
}
export default withSize()(Box);
Performance depends on how the library detects changes. Modern browsers offer ResizeObserver, which is efficient and element-specific. Older methods rely on window resize events or iframe hacks.
react-measure prioritizes ResizeObserver.
// react-measure: Uses ResizeObserver internally
// No extra config needed, optimized by default
const [ref, measurements] = useMeasure();
react-resize-aware typically uses a sensor element.
// react-resize-aware: Sensor-based detection
// Wraps children with a sensor div internally
<ResizeAware>{/* content */}</ResizeAware>
react-sizeme supports multiple strategies.
ResizeObserver or window events.// react-sizeme: Configurable strategy
export default withSize({
refreshRate: 16,
allowPlaceholder: true
})(Box);
When choosing a utility library, long-term support is critical. React evolves quickly, and libraries must keep up.
react-measure is actively maintained.
react-resize-aware has limited activity.
react-sizeme is in maintenance mode.
react-measure or native hooks when possible.You need to render a chart that redraws whenever its container resizes.
react-measureResizeObserver ensures high-frequency updates don't slow down the UI.// react-measure
function ChartContainer() {
const [ref, { width, height }] = useMeasure();
return (
<div ref={ref}>
<MyChart width={width} height={height} />
</div>
);
}
You are maintaining a large codebase with class components that need size data.
react-sizeme (if already present) or react-measurereact-sizeme HOC wraps class components easily without refactoring to hooks. However, react-measure render props also work well here.// react-sizeme
class Dashboard extends React.Component {
render() {
const { width } = this.props.size;
return <div>{width}</div>;
}
}
export default withSize()(Dashboard);
You need basic dimensions for a small tooltip component.
react-resize-aware// react-resize-aware
<ResizeAware>
{({ width }) => <Tooltip width={width} />}
</ResizeAware>
| Feature | react-measure | react-resize-aware | react-sizeme |
|---|---|---|---|
| Primary API | Hooks & Render Props | Component Wrapper | Higher-Order Component |
| Detection | ResizeObserver | Sensor Element | Configurable (Observer/Window) |
| Maintenance | Active | Low Activity | Maintenance Mode |
| React Version | Modern (16.8+) | Compatible | Legacy/Modern |
| Recommendation | ā Preferred | ā ļø Use with Care | ā Avoid for New Projects |
Before installing any package, consider if you need one at all. Modern React applications can use the ResizeObserver API directly via a custom hook or a lightweight library like @react-hook/resize-observer.
// Native approach (no extra library)
useEffect(() => {
const observer = new ResizeObserver(entries => {
for (let entry of entries) {
console.log(entry.contentRect.width);
}
});
if (ref.current) observer.observe(ref.current);
return () => observer.disconnect();
}, []);
react-measure is the clear winner for most teams today. It balances feature depth with modern API design. The Hook support makes it easy to test and compose, while the active maintenance ensures it won't break with future React updates.
react-sizeme should be treated as legacy technology. If you encounter it in an existing codebase, plan to migrate away from it during major refactors. Its HOC pattern adds unnecessary abstraction compared to Hooks.
react-resize-aware sits in the middle. It is simple but lacks the ecosystem support of react-measure. Only choose it if you have a specific constraint preventing the use of Hooks or if you need a very specific component-wrapper behavior not offered elsewhere.
Final Thought: For new architecture, prioritize react-measure or native ResizeObserver hooks. Avoid locking your project into maintenance-mode libraries like react-sizeme unless you are strictly bound by legacy constraints.
Choose react-measure for new projects requiring robust dimension tracking with modern React patterns. It supports both Hooks and Render Props, making it flexible for functional and class components. It actively leverages ResizeObserver for performance, making it suitable for complex layouts where frequent measurements occur without causing layout thrashing.
Choose react-resize-aware if you need a lightweight, component-based solution and prefer avoiding Hooks or HOCs. It is suitable for simpler use cases where you just need to wrap a component and access size props. However, verify its maintenance status for long-term projects, as it has a smaller ecosystem compared to react-measure.
Choose react-sizeme only for maintaining existing legacy applications that already rely on its Higher-Order Component pattern. It is not recommended for new development as it is in maintenance mode. If you are starting fresh, prefer react-measure or native ResizeObserver hooks to ensure long-term support and better alignment with modern React practices.
Compute measurements of React components. Uses a
ResizeObserver
to detect when an element's dimensions have changed.
Includes a
polyfill for ResizeObserver
in unsupported browsers.
yarn add react-measure
npm install react-measure --save
<script src="https://unpkg.com/react-measure/dist/index.umd.js"></script>
(UMD library exposed as `ReactMeasure`)
Wrap any child component and calculate its client rect.
client: PropTypes.boolAdds the following to contentRect.client returned in the child function.
clientTop, clientLeft, clientWidth, and clientHeight.
offset: PropTypes.boolAdds the following to contentRect.offset returned in the child function.
offsetTop, offsetLeft, offsetWidth, and offsetHeight.
scroll: PropTypes.boolAdds the following to contentRect.scroll returned in the child function.
scrollTop, scrollLeft, scrollWidth, and scrollHeight.
bounds: PropTypes.boolUses
getBoundingClientRect
to calculate the element rect and add it to contentRect.bounds returned in the
child function.
margin: PropTypes.boolUses
getComputedStyle
to calculate margins and add it to contentRect.margin returned in the child
function.
innerRef: PropTypes.funcUse this to access the internal component ref.
onResize: PropTypes.funcCallback invoked when either element width or height have changed. Note that this will be called twice on mount to get the initial values. The first call will come from componentDidMount while the second call will come from the ResizeObserver.
children: PropTypes.funcChildren must be a function. Will receive the following object shape:
measureRef: must be passed down to your component's ref in order to obtain a
proper node to measure
measure: use to programmatically measure your component, calls the internal
measure method in withContentRect
contentRect: this will contain any of the following allowed rects from
above: client, offset, scroll, bounds, or margin. It will also
include entry from the ResizeObserver when available.
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
const className = classNames(width < 400 && 'small-width-modifier')
return (
<Measure
bounds
onResize={contentRect => {
this.setState({ dimensions: contentRect.bounds })
}}
>
{({ measureRef }) => (
<div ref={measureRef} className={className}>
I can do cool things with my dimensions now :D
{height > 250 && (
<div>Render responsive content based on the component size!</div>
)}
</div>
)}
</Measure>
)
}
}
A higher-order component that provides dimensions to the wrapped component.
Accepts types, which determines what measurements are returned, similar to
above. Then returns a function to pass the component you want measured.
Pass an array or single value of either client, offset, scroll, bounds,
or margin to calculate and receive those measurements as the prop
contentRect in your wrapped component. You can also use the measure function
passed down to programmatically measure your component if you need to. And
finally, remember to pass down the measureRef to the component you want
measured.
Passes down the same props as the Measure child function above, measureRef,
measure, and contentRect.
Fun fact, the Measure component is a thin wrapper around withContentRect.
Just check
the source.
This means your wrapped component will accept the same props as Measure does
š
import { withContentRect } from 'react-measure'
const ItemToMeasure = withContentRect('bounds')(
({ measureRef, measure, contentRect }) => (
<div ref={measureRef}>
Some content here
<pre>{JSON.stringify(contentRect, null, 2)}</pre>
</div>
)
)
clone repo
git clone git@github.com:souporserious/react-measure.git
move into folder
cd ~/react-measure
install package dependencies
yarn
move into site folder and install local site dependencies
cd ~/site && yarn
run development mode
yarn gatsby develop