@react-hook/resize-observer

Concurrent-mode-safe React hook wrapping the native ResizeObserver API

Library
npm
v2.0.2
1,530 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
38 /100 Needs Attention
Development Activity 0
Maintenance 0
Community 52
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
75 /100 Good
Architecture 78
Code Quality 74
Innovation 68
Learning Curve 80

@react-hook/resize-observer is a small, strongly-typed React hook that subscribes an element to the browser’s native ResizeObserver API and invokes a callback whenever its size changes. It is one member of the react-hook monorepo — a collection of independently published, single-purpose React hooks — and is itself the foundation that higher-level hooks in the same repo (@react-hook/size, @react-hook/window-size) build on for element and viewport measurement.

Internally it maintains a single shared ResizeObserver instance per page (rather than one per component), batching entries and dispatching callbacks inside requestAnimationFrame to avoid the layout-thrashing and redundant-instance overhead of naively creating a new observer per hook call. It accepts refs, raw elements, or forwarded refs as targets, and accepts an optional polyfill for environments without native ResizeObserver support.

What You Get

  • A useResizeObserver(target, callback, options) hook accepting a ref, forwarded ref, or raw DOM element as the target
  • A shared, singleton ResizeObserver instance reused across every hook call on the page instead of one per component
  • requestAnimationFrame-batched callback dispatch, so many simultaneous resizes don’t fire callbacks synchronously per entry
  • An optional polyfill option for browsers without native ResizeObserver support
  • Foundation for the sibling @react-hook/size and @react-hook/window-size hooks published from the same monorepo

Common Use Cases

  • Measuring an element’s rendered dimensions to drive responsive internal layout (e.g. switching a chart’s orientation based on container width)
  • Building custom size-tracking hooks (as @react-hook/size does) on top of a shared, efficient observer primitive
  • Virtualized lists or canvases that must re-measure their container whenever it resizes (window resize, sidebar toggle, split-pane drag)
  • Component libraries that need per-instance resize callbacks without each instance instantiating its own native ResizeObserver

Under The Hood

Architecture - The hook itself (packages/resize-observer/src/index.tsx) is a thin subscription layer: getResizeObserver() lazily creates one module-level ResizeObserver via createResizeObserver(), which maintains a Map of target elements to arrays of callbacks. The native observer’s own callback accumulates all entries received in a tick, and on the first entry schedules a single requestAnimationFrame flush that dispatches each target’s most recent entry to its subscribed callbacks, deduplicating targets that resized multiple times in the same frame. The hook subscribes/unsubscribes through useLayoutEffect (from the sibling @react-hook/passive-layout-effect package) and stores the latest callback via @react-hook/latest to avoid stale closures without re-subscribing on every render.

Tech Stack - TypeScript, built with lundle (multi-format ESM/CJS/UMD bundler) into dist/main, dist/module, dist/esm, and dist/umd outputs plus hand-maintained .d.ts types; part of a Yarn/Lerna-style monorepo (packages/*) with no build-tool coupling to the other hooks beyond declared npm dependencies (@react-hook/latest, @react-hook/passive-layout-effect).

Code Quality - A single src/index.test.tsx under Jest with @testing-library/react covers the hook’s subscribe/unsubscribe behavior; the implementation is compact (~105 lines) with clear separation between the public hook and the internal shared-observer factory. Repository-wide activity has slowed (last publish mid-2024, low recent commit velocity per the health scan), so it should be treated as stable-but-low-maintenance rather than actively evolving.

API Design - The useResizeObserver(target, callback, options) signature accepts refs, forwarded refs, or raw elements interchangeably, minimizing friction for both function and class-adjacent (forwardRef) component patterns. Returning the raw shared ResizeObserver instance from the hook is a deliberate escape hatch for advanced consumers who need direct access, while the default path requires zero manual cleanup since unsubscription is handled by the useLayoutEffect return function.

Used by 6 apps in this directory

TypeScript
96%
Other

Amplication

AI Code Assistants · Automation · Developer Tools

16,015

Create production-ready backend services with your organization's standards baked in — generating NestJS, Prisma, and GraphQL code that you own and control.

View details
58
Repo Health
84
Technical
60
Dependency
Built with
TypeScript 96%
Updated 3 months ago
Rust
67%
Other

DefGuard

Authentication · Networking · Security

2,849

Self-hosted secure remote access that unifies WireGuard VPN, identity management, and connection-level MFA in one open-source platform.

View details
84
Repo Health
82
Technical
72
Dependency
Built with
Rust 67%
TypeScript 31%
Updated 1 weeks ago
TypeScript
88%
MIT

medusa

Ecommerce

36,494

The most flexible open-source commerce platform — build B2C, B2B, and marketplace applications with modular, composable commerce primitives.

View details
93
Repo Health
87
Technical
63
Dependency
Built with
TypeScript 88%
JavaScript 12%
Updated 1 weeks ago
Python
55%
Other

PostHog

Ab Testing Experimentation · Analytics · Developer Tools

39,975

The all-in-one open source product platform combining analytics, session replay, feature flags, error tracking, AI observability, and a built-in data warehouse in a single self-hostable stack.

View details
92
Repo Health
80
Technical
65
Dependency
Built with
Python 55%
TypeScript 36%
Updated 5 days ago
TypeScript
89%
Other

Hexclave

Authentication · Developer Tools

6,862

The open-source user infrastructure platform — authentication, teams, payments, emails, analytics, and more on a single unified user model.

View details
87
Repo Health
80
Technical
66
Dependency
Built with
TypeScript 89%
Updated 1 weeks ago
TypeScript
98%
Apache 2.0

TinaCMS

CMS

13,811

An open-source, Git-backed headless CMS that gives editors a live visual editing UI over Markdown, MDX, JSON, and YAML content while developers keep everything in version control.

View details
92
Repo Health
74
Technical
59
Dependency
Built with
TypeScript 98%
Updated 5 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers