This package wraps Intersection Observer with hooks and components for scroll-triggered UI, lazy loading, and visibility tracking. Shared observers keep many targets efficient.
Overview
useInView returns a ref, boolean visibility state, and the latest entry object, matching native observer options for root, margins, and thresholds. useOnInView offers a callback-oriented variant that avoids re-renders on each crossing.
A component API supports render props, plain child wrappers, and onChange handlers with the same option surface. Observers with identical settings are reused internally to limit overhead on long lists.
TypeScript types ship with the package, and testing utilities mock observers for Jest or Vitest. The implementation tree-shakes so importing hooks pulls only needed code.
Initial false notifications from the underlying API are suppressed so handlers fire after genuine visibility changes, matching documented behavior for hooks and components alike.
Key features
- useInView hook with ref
- useOnInView callback hook
- Render-prop component mode
- Observer instance sharing
- Full IntersectionObserver options
- TypeScript definitions included
- Test environment mocks
- Small gzipped bundle size
Use cases
- Lazy-loading images and sections
- Scroll-driven animations
- Infinite scroll sentinels
- Ad or content impression tracking
- Deferring work until elements appear