
Security News
Insecure Agents Podcast: Certified Patches, Supply Chain Security, and AI Agents
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.
react-use-measure
Advanced tools
yarn add react-use-measure
This small tool will measure the boundaries (for instance width, height, top, left) of a view you reference. It is reactive and responds to changes in size, window-scroll and nested-area-scroll.
Because there is no simple way to just get relative view coordinates. Yes, there is getBoundingClientRect, but it does not work when your content sits inside scroll areas whose offsets are simply neglected (as well as page scroll). Worse, mouse coordinates are relative to the viewport (the visible rect that contains the page). There is no easy way, for instance, to know that the mouse hovers over the upper/left corner of an element. This hook solves it for you.
You can try a live demo here: https://codesandbox.io/s/musing-kare-4fblz
import useMeasure from 'react-use-measure'
function App() {
const [ref, bounds] = useMeasure()
// consider that knowing bounds is only possible *after* the view renders
// so you'll get zero values on the first run and be informed later
return <div ref={ref} />
}
interface RectReadOnly {
readonly x: number
readonly y: number
readonly width: number
readonly height: number
readonly top: number
readonly right: number
readonly bottom: number
readonly left: number
}
type Options = {
// Debounce events in milliseconds
debounce?: number | { scroll: number; resize: number }
// React to nested scroll changes, don't use this if you know your view is static
scroll?: boolean
// You can optionally inject a resize-observer polyfill
polyfill?: { new (cb: ResizeObserverCallback): ResizeObserver }
// Measure size using offsetHeight and offsetWidth to ignore parent scale transforms
offsetSize?: boolean
}
useMeasure(
options: Options = { debounce: 0, scroll: false }
): [React.MutableRefObject<HTMLElement | SVGElement>, RectReadOnly]
This lib relies on resize-observers. If you need a polyfill you can either polute the window object or inject it cleanly using the config options. We recommend @juggle/resize-observer.
import { ResizeObserver } from '@juggle/resize-observer'
function App() {
const [ref, bounds] = useMeasure({ polyfill: ResizeObserver })
useMeasure currently returns its own ref. We do this because we are using functional refs for unmount tracking. If you need to have a ref of your own on the same element, use react-merge-refs.
react-measure is a similar package that provides a higher-order component (HOC) and a render prop component for measuring the size and position of DOM elements. It offers more flexibility in terms of how you can use it (HOC, render prop, or hook), but it may be slightly more complex to set up compared to react-use-measure.
react-resize-detector is another package that provides a way to detect changes in the size of a DOM element. It offers a hook, a render prop component, and a HOC for flexibility. It focuses more on detecting resize events rather than providing detailed measurements, making it simpler but less feature-rich compared to react-use-measure.
resize-observer-polyfill is a polyfill for the ResizeObserver API, which allows you to observe changes to the size of an element. While not specifically a React package, it can be used in React applications to achieve similar functionality. It provides a lower-level API compared to react-use-measure, requiring more manual setup.
FAQs
Utility to measure view bounds
The npm package react-use-measure receives a total of 1,157,789 weekly downloads. As such, react-use-measure popularity was classified as popular.
We found that react-use-measure demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.

Security News
Tailwind Labs laid off 75% of its engineering team after revenue dropped 80%, as LLMs redirect traffic away from documentation where developers discover paid products.

Security News
The planned feature introduces a review step before releases go live, following the Shai-Hulud attacks and a rocky migration off classic tokens that disrupted maintainer workflows.