Skip to content

useInView

This hook uses useIntersectionObserver hook and returns the intersecting state.

Options

Return Values

NameTypeDescription
refRefObjectthe target element reference.
isInViewBooleanwhether the target element is in the view or not, if set once to true, it will not change after the target is intersecting.

Example Usage

tsx
import { useInView } from 'react-pre-hooks';

export default function Example() {
  const { ref, isInView } = useInView({ threshold: 0.85 });

  return (
    <main>
      <p>Scroll down..</p>
      <div ref={ref} className={isInView ? "show" : ""}></div>
    </main>
  );
}
import { useInView } from 'react-pre-hooks';

export default function Example() {
  const { ref, isInView } = useInView({ threshold: 0.85 });

  return (
    <main>
      <p>Scroll down..</p>
      <div ref={ref} className={isInView ? "show" : ""}></div>
    </main>
  );
}

Released under the MIT License.