Overview
Observes size changes of a target element and returns its latest layout rectangle. Observation updates are scheduled inrequestAnimationFrame to avoid excessive React updates.
Function Signature
Type Definitions
Parameters
Options forwarded to
ResizeObserver.observe. Can include box property to specify which box model to observe ("content-box", "border-box", or "device-pixel-content-box").Return Value
A ref callback to attach to the element you want to observe.
An object containing the element’s dimensions and position:
width: Element width in pixelsheight: Element height in pixelsx,y: Position relative to viewporttop,left,bottom,right: Edges relative to viewport
Usage Example
useElementSize
A convenience wrapper arounduseResizeObserver that exposes only width and height.
Function Signature
Type Definitions
Usage Example
Features
- Uses native
ResizeObserverAPI for efficient size tracking - Throttles updates with
requestAnimationFrameto prevent layout thrashing - Automatic cleanup when element is removed
- Supports all
ResizeObserverbox models - Convenience
useElementSizewrapper for simple width/height needs