- Hooks
- Elements hooks
- useIntersectionObserver
useIntersectionObserver
Track and manage the visibility of your DOM elements within the viewport with useIntersectionObserver.
use-intersection-observer.tsx
Installation
Copy and paste the following code into your project.
use-intersection-observer.ts
Update the import paths to match your project setup.
PARAMETERS
Name | Type | Description |
---|---|---|
threshold | number | Either a single number or an array of numbers between 0 and 1, indicating at what percentage of the target’s visibility the observer’s callback should be executed. |
root | element | The Element that is used as the viewport for checking visibility of the target. Defaults to the browser viewport if not specified or if null. |
rootMargin | string | Margin around the root. Can have values similar to the CSS margin property. The values can be percentages. This set of values serves to grow or shrink each side of the root element’s bounding box before computing intersections. Defaults to all zeros. |
Return values
Name | Type | Description |
---|---|---|
ref | React.ref.object | A React reference that can be attached to a DOM element to observe. |
entry | object | An object containing information about the intersection. This object is similar to IntersectionObserverEntry. |