Custom React hook to detect when an element is visible in the viewport. Uses the Intersection Observer API.
Reference to the target element.
Optional
Visibility ratio threshold (between 0.0 and 1.0).
0.0
1.0
true
false
If ratio is not between 0.0 and 1.0.
ratio
// Basic usage: Check if an element is fully visibleconst ref = useRef(null);const isVisible = useOnScreen(ref);<div ref={ref}>This element is {isVisible ? "visible" : "not visible"}</div>; Copy
// Basic usage: Check if an element is fully visibleconst ref = useRef(null);const isVisible = useOnScreen(ref);<div ref={ref}>This element is {isVisible ? "visible" : "not visible"}</div>;
// Check if an element is at least 50% visibleconst ref = useRef(null);const isHalfVisible = useOnScreen(ref, 0.5); Copy
// Check if an element is at least 50% visibleconst ref = useRef(null);const isHalfVisible = useOnScreen(ref, 0.5);
Custom React hook to detect when an element is visible in the viewport. Uses the Intersection Observer API.