# useInView (/docs/hooks/use-in-view)





`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.

```tsx
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 [#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.

<Preview code="&#x22;const root = useRef(null);\nconst end = useRef(null);\nconst endInView = useInView(end, { root });\n\nfunction Row({ root }) {\n  const ref = useRef(null);\n  const inView = useInView(ref, { root, threshold: 0.5, once: true });\n  // Hide only what is known to be out of view.\n  return (\n    <li ref={ref} className={inView === false ? 'motion-safe:opacity-0' : undefined}>\n      …\n    </li>\n  );\n}\n\n<div ref={root} className=\&#x22;h-56 overflow-auto\&#x22;>\n  <ul>{rows}</ul>\n  <p ref={end}>The end.</p>\n</div>&#x22;">
  <InViewDemo />
</Preview>

## Not observed yet [#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.

```tsx
<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 [#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.

```tsx
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 [#api]

<TypeTable
  type="{
  ref: {
    type: 'RefObject<Element | null>',
    required: true,
    description: 'Ref of the element to watch.',
  },
  'options.root': {
    type: 'RefObject<Element | null>',
    description: 'The scrolling ancestor to intersect with. Without it, the viewport.',
  },
  'options.margin': {
    type: 'string',
    default: '&#x22;0px&#x22;',
    description: 'Grows (positive) or shrinks (negative) the root\'s box before testing, in CSS margin syntax. Percentages are of the root\'s size.',
  },
  'options.threshold': {
    type: 'number | number[]',
    default: '0',
    description: 'How much of the element must show to count as in view, from 0 (one pixel) to 1 (all of it). With an array, the smallest value decides.',
  },
  'options.once': {
    type: 'boolean',
    default: 'false',
    description: 'Stops observing the first time the element comes into view, so the value stays true.',
  },
}"
/>

Returns `boolean | undefined`: `true` in view, `false` out of view, `undefined` until the first observation.
