Overview
A React implementation of the Intersection Observer API that tells you when an element enters or leaves the viewport. Use hooks for React state or effects, or <InView> for render props and plain children.
Use it to lazy-load content, trigger scroll-based animations, track impressions, prefetch data, highlight visible sections, and load more results.
Start simple
npm install react-intersection-observerpnpm add react-intersection-observeryarn add react-intersection-observerbun add react-intersection-observerimport { useInView } from "react-intersection-observer";
export function Section() {
const { ref, inView } = useInView();
return <section ref={ref}>{inView ? "In view" : "Waiting"}</section>;
}
Attach ref to an element and use inView in the render. By default, the browser viewport is the root and any intersection changes the state. Configure a threshold, margin, or custom root only when that default is not enough.
When you need geometry, entry is the latest observer result; it is undefined until the browser accepts a notification.
Choose an API
| When you need to | Use | Why |
|---|---|---|
| Change what a component renders | useInView |
Returns a ref, inView, and the latest entry. |
| Run an impression, prefetch, or analytics callback | useOnInView |
Calls your callback without a hook-owned state update. |
| Keep observation close to render props or a wrapper | <InView> |
Provides render props and supports plain children. |
See it work
The basic hook above uses the browser viewport. This demo adds a custom scroll root and a threshold so you can see how options change the same inView state.
Built for production React
- Native and efficient. Familiar browser options and shared observer instances keep many observed elements inexpensive.
- Typed and composable. Hooks, components, options, and observer entries are typed without another package.
- Testable at the right layer. Use deterministic utilities for transitions and Browser Mode for real browser behavior.
- Focused exports. Tree shaking keeps the client footprint limited to the APIs you import.
Common next steps
- Core APIs — learn the full contracts and return values.
- Observer options — tune thresholds, margins, and scroll containers.
- Recipes — build lazy loading, reveals, impressions, and infinite lists.
For verification, see Testing; for server-rendered apps, see SSR and fallbacks.