React Intersection Observer Hook
The `react-intersection-observer-hook` package provides declarative React hooks, specifically `useIntersectionObserver` and `useTrackVisibility`, to interact with the browser's native Intersection Observer API. It simplifies detecting when a React component enters or exits the viewport, enabling features like lazy loading, infinite scrolling, and triggering animations. The current stable version is 4.0.2, with active development focusing on modern React compatibility, demonstrated by its recent update for React 19 in v4.0.0. Releases are frequent, addressing bugs and improving API ergonomics. Key differentiators include a dedicated `useTrackVisibility` for simpler boolean visibility checks, explicit `rootRef` handling for scrollable containers, and robust support for both ES Modules and CommonJS environments since v3.0.0, ensured through modern bundling practices.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'isIntersecting')
cause The `entry` object from `useIntersectionObserver` or `useTrackVisibility` is `undefined` during the initial render before the observer has reported an intersection.fixAlways guard access to `entry` properties, e.g., `const isVisible = entry && entry.isIntersecting;`. -
Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
cause Using `useIntersectionObserver` or `useTrackVisibility` outside a React function component or custom hook, or having multiple React versions in your dependency tree.fixEnsure the hook is called within a React function component or another custom hook, and verify `react` and `react-dom` peer dependencies match your project's React version (especially for v4 requiring React 19). -
Module not found: Can't resolve 'react-intersection-observer-hook'
cause Your build tool or Node.js environment is struggling to resolve the package's module exports, potentially due to `v3.0.0`'s bundling changes and stricter ESM/CJS output.fixCheck your `tsconfig.json` (`moduleResolution`, `module`), `package.json` (`type`), and bundler configuration. Ensure you're using a modern bundler setup that supports `exports` field in `package.json`.
Warnings
- breaking Versions `v4.0.0` and above are designed for React 19 and utilize its new cleanup functions for refs. Users on older React versions (e.g., React 18 or earlier) should continue using `v3.x` to avoid compatibility issues.
- breaking Version `v3.0.0` introduced significant internal changes, including a migration to a monorepo structure, bundling with tsup, and stricter ESM/CJS compliance. While this improves module support, users with highly customized or older build setups might encounter resolution issues.
- gotcha The `ref` callback returned by `useIntersectionObserver` and `useTrackVisibility` must be attached to *only one* element at a time. Attaching it to multiple elements will result in unexpected behavior or only track the last assigned element.
- gotcha When specifying a scrollable container as the `root`, the `rootRef` callback returned by the hook should be used to assign the container element, rather than attempting to pass a `root` DOM element directly as an argument to the hook.
- deprecated As of `v4.0.2`, the README no longer suggests using a polyfill for `IntersectionObserver`. Modern browsers widely support the API, and polyfills are rarely needed unless targeting legacy environments.
Install
-
npm install react-intersection-observer-hook -
yarn add react-intersection-observer-hook -
pnpm add react-intersection-observer-hook
Imports
- useIntersectionObserver
const useIntersectionObserver = require('react-intersection-observer-hook');import { useIntersectionObserver } from 'react-intersection-observer-hook'; - useTrackVisibility
import useTrackVisibility from 'react-intersection-observer-hook';
import { useTrackVisibility } from 'react-intersection-observer-hook'; - IntersectionObserverHookArgs
import type { IntersectionObserverHookArgs } from 'react-intersection-observer-hook';
Quickstart
import React from 'react';
import { useTrackVisibility } from 'react-intersection-observer-hook';
function MyTrackedComponent() {
const [ref, { isVisible, entry, rootRef }] = useTrackVisibility({
// Optionally, track visibility only once
// once: true,
// Customize intersection threshold (0.0 to 1.0)
threshold: 0.5,
// Add margin around the root (e.g., '10px 20px 30px 40px')
// rootMargin: '0px 0px -50px 0px',
});
// For a scrollable container (e.g., div with overflow-y: scroll),
// attach rootRef to the container. Otherwise, it defaults to the viewport.
const ScrollableContainer = React.forwardRef((props, forwardedRef) => (
<div ref={forwardedRef} style={{ height: '300px', overflowY: 'scroll', border: '1px solid gray' }}>
{props.children}
</div>
));
return (
<div style={{ height: '1000px', padding: '200px 0' }}>
<p>Scroll down to see the tracked element.</p>
<ScrollableContainer ref={rootRef}>
<div style={{ height: '400px', background: 'lightblue', margin: '100px 0' }}></div>
<div ref={ref} style={{ height: '200px', background: isVisible ? 'lightgreen' : 'lightcoral', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
<h2>Element is {isVisible ? 'visible!' : 'not visible.'}</h2>
{entry && <p>Intersection Ratio: {entry.intersectionRatio.toFixed(2)}</p>}
</div>
<div style={{ height: '400px', background: 'lightblue', margin: '100px 0' }}></div>
</ScrollableContainer>
</div>
);
}
export default MyTrackedComponent;