Skip to content
usefy

useResizeObserver

Layout & observers

A powerful React hook for observing element size changes using the Resize Observer API

Install

$ npm install @usefy/use-resize-observer

Quick start

useResizeObserver.tsx
import { useResizeObserver } from "@usefy/use-resize-observer";

function MyComponent() {
  const { ref, width, height } = useResizeObserver();

  return (
    <div ref={ref}>
      Size: {width} x {height}
    </div>
  );
}

API reference

useResizeObserver(options?)

A hook that observes element size changes using the Resize Observer API.

Parameters

ParameterTypeDescription
optionsUseResizeObserverOptionsOptional configuration object

Options

OptionTypeDefaultDescription
boxResizeObserverBoxOptions"content-box"Box model to observe: "content-box", "border-box", or "device-pixel-content-box"
debouncenumber0Debounce delay in milliseconds. Waits until resizing stops before updating
throttlenumber0Throttle interval in milliseconds. Updates at most once per interval
round(value: number) => numberMath.roundFunction to round size values. Use Math.floor, Math.ceil, or custom function
enabledbooleantrueEnable/disable observer. When false, observer disconnects and stops all updates
updateStatebooleantrueWhether to update React state. Set to false for callback-only mode
initialWidthnumberInitial width value for SSR or before first observation
initialHeightnumberInitial height value for SSR or before first observation
onResizeOnResizeCallbackCallback fired on every resize with entry, width, and height
onErrorOnErrorCallbackCallback fired when ResizeObserver encounters an error

Returns UseResizeObserverReturn

PropertyTypeDescription
ref(node: Element | null) => voidCallback ref to attach to the target element you want to observe
widthnumber | undefinedCurrent width of the observed element
heightnumber | undefinedCurrent height of the observed element
entryResizeEntry | undefinedFull resize entry data (undefined if not yet observed)
isSupportedbooleanWhether ResizeObserver API is supported in the current environment
observe(target: Element) => voidManually start observing a specific element
unobserve(target: Element) => voidStop observing a specific element
disconnect() => voidDisconnect the observer completely

ResizeEntry

Extended resize entry with convenience properties:

PropertyTypeDescription
entryResizeObserverEntryOriginal native ResizeObserverEntry from the browser API
targetElementThe observed DOM element
contentRectDOMRectReadOnlyContent rectangle (same as contentRect from native entry)
borderBoxSizeResizeObserverSize[]Border box dimensions array
contentBoxSizeResizeObserverSize[]Content box dimensions array
devicePixelContentBoxSizeResizeObserverSize[]Device pixel content box dimensions (if supported)

ResizeObserverBoxOptions

type ResizeObserverBoxOptions =
  | "content-box"              // Content area only (default)
  | "border-box"               // Content + padding + border
  | "device-pixel-content-box" // Physical pixels (for canvas)

Go deeper

This page is the quick reference. For every example, prop, and edge case, read the full README — or open Storybook to change props live.

More in layout & observers