@react-hook/resize-observer

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

Library
npm
v2.0.2
1,530stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
38/100Needs Attention
Development Activity0
Maintenance0
Community52
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
75/100Good
Architecture78
Code Quality74
Innovation68
Learning Curve80

@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

Developer Tools · AI Code Assistants · Automation

16,011

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
60
Repo Health
84
Technical
60
Dependency
Built with
TypeScript96%
Updated 2 months ago
Rust
67%
Other

DefGuard

Security · Networking · Authentication

2,819

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
Rust67%
TypeScript31%
Updated 3 days ago
TypeScript
86%
MIT

medusa

Ecommerce

36,163

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
TypeScript86%
JavaScript14%
Updated 2 days ago
Python
54%
Other

PostHog

Analytics · Monitoring · Developer Tools

39,612

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
Python54%
TypeScript36%
Updated today
TypeScript
92%
Other

Hexclave

Developer Tools · Authentication

6,854

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
TypeScript92%
Updated 2 days ago
TypeScript
98%
Apache 2.0

TinaCMS

CMS

13,780

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
TypeScript98%
Updated today

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

Search