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

@react-hook/intersection-observer

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@react-hook/intersection-observer

A React hook for the IntersectionObserver API that uses a polyfill when the native API is not available

  • 2.0.10
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
137K
increased by7.39%
Maintainers
1
Weekly downloads
 
Created

What is @react-hook/intersection-observer?

@react-hook/intersection-observer is a React hook that allows you to easily observe when an element enters or leaves the viewport. It leverages the Intersection Observer API to provide a simple and efficient way to handle visibility changes of DOM elements.

What are @react-hook/intersection-observer's main functionalities?

Basic Intersection Observer

This example demonstrates how to use the `useIntersectionObserver` hook to change the background color of a div based on whether it is in the viewport.

```jsx
import React from 'react';
import useIntersectionObserver from '@react-hook/intersection-observer';

const Component = () => {
  const targetRef = React.useRef(null);
  const { isIntersecting } = useIntersectionObserver(targetRef);

  return (
    <div>
      <div ref={targetRef} style={{ height: '100px', background: isIntersecting ? 'green' : 'red' }}>
        {isIntersecting ? 'In view' : 'Out of view'}
      </div>
    </div>
  );
};

export default Component;
```

Thresholds and Root Margin

This example shows how to configure the `useIntersectionObserver` hook with a threshold and root margin. The background color changes when at least 50% of the target element is in view.

```jsx
import React from 'react';
import useIntersectionObserver from '@react-hook/intersection-observer';

const Component = () => {
  const targetRef = React.useRef(null);
  const { isIntersecting } = useIntersectionObserver(targetRef, {
    threshold: 0.5,
    rootMargin: '0px 0px -50px 0px'
  });

  return (
    <div>
      <div ref={targetRef} style={{ height: '100px', background: isIntersecting ? 'blue' : 'orange' }}>
        {isIntersecting ? '50% in view' : 'Less than 50% in view'}
      </div>
    </div>
  );
};

export default Component;
```

Other packages similar to @react-hook/intersection-observer

Keywords

FAQs

Package last updated on 26 Nov 2019

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