useInView
Tracks whether an element is scrolled into view, through an IntersectionObserver — for lazy media, infinite lists and entrances that play once.
useInView tracks whether an element is scrolled into view, updating live through an IntersectionObserver. Reach for it to load media only when it's about to be seen, to fetch the next page when an infinite list nears its end, or to play an entrance once. Spotlight's paginated results work this way.
import { useInView } from '@42-central/ui-react';
import { useRef } from 'react';
const ref = useRef<HTMLDivElement>(null);
const inView = useInView(ref, { margin: '200px', once: true });
<div ref={ref}>{inView && <img src={src} alt="" />}</div>;Usage
Scroll the box below. Each row fades up the first time half of it shows (once), and the line above follows the end of the list in and out of view.
End of the list: not observed yet
- Row 1
- Row 2
- Row 3
- Row 4
- Row 5
- Row 6
- Row 7
- Row 8
- Row 9
- Row 10
- Row 11
- Row 12
- Row 13
- Row 14
- Row 15
- Row 16
The end.
Code
const root = useRef(null);
const end = useRef(null);
const endInView = useInView(end, { root });
function Row({ root }) {
const ref = useRef(null);
const inView = useInView(ref, { root, threshold: 0.5, once: true });
// Hide only what is known to be out of view.
return (
<li ref={ref} className={inView === false ? 'motion-safe:opacity-0' : undefined}>
…
</li>
);
}
<div ref={root} className="h-56 overflow-auto">
<ul>{rows}</ul>
<p ref={end}>The end.</p>
</div>Not observed yet
The hook returns undefined until its first observation: on the server, on the first client render, and in an environment without IntersectionObserver. A real browser corrects it right after mount, since the observer reports the element's state as soon as it starts watching.
That third state is what makes an entrance safe. Hide only what is known to be out of view, inView === false, and nothing is ever hidden without JavaScript, while an element already on screen at load goes straight from undefined to true and never flashes out and back in. Use motion-safe: for the hidden state, so a reader who asked for reduced motion just sees the content.
<div
ref={ref}
className={cn(
'motion-safe:transition-[opacity,translate] motion-safe:duration-500',
inView === false && 'motion-safe:translate-y-3 motion-safe:opacity-0',
)}
/>For lazy media, undefined is falsy, so inView && <img /> waits for the browser like you'd expect.
Infinite lists
Put a sentinel after the last row and load the next page when it shows. With the list as the root and a bottom margin, the request starts a few rows before the end. Give the sentinel a key that changes with each page, so it remounts and reports a fresh true even if the new rows didn't push it out of view.
function Sentinel({ root, onEnd }) {
const ref = useRef(null);
const inView = useInView(ref, { root, margin: '0px 0px 160px 0px' });
useEffect(() => {
if (inView) onEnd();
}, [inView, onEnd]);
return <div ref={ref} aria-hidden />;
}
<Sentinel key={rows.length} root={listRef} onEnd={loadMore} />;API
Prop
Type
Returns boolean | undefined: true in view, false out of view, undefined until the first observation.