Skip to Content
HooksuseOnScreen

useOnScreen

useOnScreen is the boolean-only visibility hook in react-rsc-kit. It is built on useIntersectionObserver but keeps the API focused when you already have a ref and only need a yes-or-no answer.

Live Example

Boolean visibility

This demo tracks a single element inside a scroll container and only exposes whether it is visible.

Boolean visibility

Is the card on screen?

Not visible

Scroll inside the container. This version only gives you a boolean, which is ideal for simple reveal or lazy-render decisions.

Keep scrolling
Section is off screen

Move the card into view to flip the state.

Bottom spacer

Import

import { useOnScreen } from "react-rsc-kit/client";

Signature

const isVisible = useOnScreen(ref, options?);

Parameters

NameTypeDefaultDescription
refRefObject<T | null>requiredRef for the element you want to observe.
options.rootElement | Document | RefObject<Element | null> | nullnullCustom observer root.
options.rootMarginstring"0px"Root margin passed to the observer.
options.thresholdnumber | number[]0Visibility threshold or thresholds.
options.oncebooleanfalseFriendly alias for freezing after the first visible intersection.
options.freezeOnceVisiblebooleanfalseStops observing after the first visible intersection.
options.triggerOncebooleanfalseLegacy compatibility alias for one-time observation.
options.disabledbooleanfalsePrevents observation without unmounting.
options.initialIsVisiblebooleanfalseInitial visibility before the first observer update.
options.fallbackInViewbooleanundefinedFallback boolean when IntersectionObserver is unavailable.

Returns

  • isVisible: true when the element is intersecting the viewport, otherwise false.

Usage

"use client"; import { useRef } from "react"; import { useOnScreen } from "react-rsc-kit/client"; export function OnScreenSection() { const ref = useRef<HTMLDivElement | null>(null); const isVisible = useOnScreen(ref, { threshold: 0.25, once: true, }); return ( <div> <div style={{ height: "100vh" }}>Scroll down</div> <div ref={ref}>{isVisible ? "Visible" : "Not visible yet"}</div> </div> ); }

Notes

  • Use this when you only need a boolean visibility flag and already have a target ref.
  • Prefer once for the simplest reveal-once behavior.
  • Use useIntersectionObserver when you need entry details, callback refs, or observer callbacks.
Last updated on