Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-intersection-observer

Package Overview
Dependencies
Maintainers
1
Versions
160
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-intersection-observer

Monitor if a component is inside the viewport, using IntersectionObserver API

  • 9.8.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.8M
increased by1.1%
Maintainers
1
Weekly downloads
 
Created

What is react-intersection-observer?

The react-intersection-observer package is a React implementation of the Intersection Observer API, making it easier to perform actions based on how an element intersects with the viewport. It's useful for lazy loading, infinite scroll, or triggering animations when the user scrolls.

What are react-intersection-observer's main functionalities?

Observing visibility of an element

This feature allows you to track the visibility of a component or element. The `useInView` hook returns a `ref` that you attach to the element you want to observe. It also returns a state `inView` that tells you whether the element is in the viewport or not.

import { useInView } from 'react-intersection-observer';

function Component() {
  const { ref, inView } = useInView();

  return (
    <div ref={ref}>
      {inView ? 'In view!' : 'Not in view!'}
    </div>
  );
}

Triggering animations when entering the viewport

This code sample demonstrates how to use the `useInView` hook to trigger animations or add classes to an element once it enters the viewport. The `threshold` option specifies how much of the element should be visible before `inView` becomes true.

import { useInView } from 'react-intersection-observer';
import { useEffect } from 'react';

function AnimatedComponent() {
  const { ref, inView } = useInView({ threshold: 0.1 });

  useEffect(() => {
    if (inView) {
      // Trigger animation or add class
    }
  }, [inView]);

  return <div ref={ref}>Animate me!</div>;
}

Other packages similar to react-intersection-observer

Keywords

FAQs

Package last updated on 09 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc