VisorVisor
Hooks

useIntersectionObserver

Tracks whether an element is visible within the viewport or a scroll container.

Usage

const { ref, isIntersecting } = useIntersectionObserver({ once: true, threshold: 0.5 })
return <div ref={ref}>{isIntersecting ? "Visible" : "Hidden"}</div>

Installation

npx visor add use-intersection-observer

Parameters

NameTypeRequiredDefaultDescription
oncebooleanNofalseIf true, stops observing after the element first intersects.
thresholdnumber | number[]No0IntersectionObserver visibility threshold.
rootElement | nullNonullScroll container to use as the intersection root.
rootMarginstringNo"0%"Margin around the root for expanding or shrinking the intersection area.

Returns

NameTypeDescription
refReact.RefObject<Element | null>Ref to attach to the element you want to observe.
isIntersectingbooleanWhether the observed element is currently intersecting.
entryIntersectionObserverEntry | nullThe raw IntersectionObserverEntry, or null before first observation.