Skip to Content
ohooks

useResizeObserver

Observes size changes on a target DOM element using the ResizeObserver API.

Loading preview...

The useResizeObserver hook observes a target DOM element via ResizeObserver, returns the latest ResizeObserverEntry (or invokes a custom onResize callback without triggering state updates), and cleans up automatically on unmount.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useResizeObserver.json

Usage

Examples

Reading the Returned ResizeObserverEntry

Omit the callback argument (or pass an options object as the second argument) to read the reactive ResizeObserverEntry directly from the return value.

Callback-Only Mode for Imperative Updates

Pass a callback function as the second argument to handle resize events imperatively (such as resizing a chart or canvas) without re-rendering the component on every pixel change.

API Reference

UseResizeObserverOptions

Configuration options for observed box model and observer activation.

Property
Specification
enabled?
uniondefault:true
Whether the ResizeObserver is active
box?
union