useIntersectionObserver()
The
useIntersectionObserver() hook provides a declarative way to track the visibility of a DOM element within the viewport. Itβs ideal for lazy loading, infinite scrolls, or triggering animations.Import
typescript
Usage
Scroll inside the box below β
Is IntersectingNO
Intersection Ratio0.00
Component.tsx
API
Arguments
Accepts an optional object with the following properties:
thresholdβ number | number[] (Optional) A number between 0 and 1 indicating the percentage of the target that should be visible before triggering. Defaults to 0.rootβ Element | Document | null (Optional) The element used as the viewport. Defaults to the browser viewport.rootMarginβ string (Optional) Margin around the root. Defaults to β0%β.freezeOnceVisibleβ boolean (Optional) If true, the observer will disconnect after the element becomes visible once. Defaults to false.
Returns
Returns a tuple (array) containing:
[0] refβ (node: Element | null) => void A callback ref to attach to the target element.[1] entryβ IntersectionObserverEntry | null The full IntersectionObserverEntry object.