useMeasure

Effortlessly measure and track your component’s dimensions with useMeasure.

Install:

npm i @uidotdev/usehooks

Description:

The useMeasure hook provides a convenient and efficient way to monitor and respond to changes in the size of a React component. This custom hook uses the ResizeObserver API to actively track changes in the component’s dimensions, such as width and height, and keeps them available as state. The returned ref is used on the element whose dimensions you want to measure, making it a valuable tool for responsive design and dynamic layout adjustments.

Return Value

NameTypeDescription
refReact ref objectA React reference that can be attached to a DOM element to observe.
rectobjectAn object containing the width and height of the observed element.

rect Object Properties

PropertyTypeDescription
widthnumberWidth of the observed element.
heightnumberHeight of the observed element.

Demo:

Example:

More Hooks:

Sort: