OptionalonVisible: () => voidOptional callback invoked once when the observed element becomes visible.
A ref object to attach to the observed element (e.g. <div ref={ref}>).
import { useVisibility } from "naystack/client";
function LazySection() {
const [loaded, setLoaded] = useState(false);
const ref = useVisibility(() => setLoaded(true));
return (
<section ref={ref}>
{loaded ? <HeavyContent /> : <Placeholder />}
</section>
);
}
Tracks when a DOM element enters the viewport and calls a callback. Uses
IntersectionObserverwithrootMargin: "100px"andthreshold: 0.1.Attach the returned ref to the element you want to observe.