react-in-viewport vs react-intersection-observer vs react-visibility-sensor
React Visibility Detection Libraries
react-in-viewportreact-intersection-observerreact-visibility-sensorSimilar Packages:

React Visibility Detection Libraries

These libraries are designed to help developers determine when a component is visible within the viewport or a specific container. This functionality is crucial for optimizing performance, lazy loading images, triggering animations, and improving user experience by only rendering elements when they are in view. Each library offers different approaches and features to handle visibility detection effectively.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
react-in-viewport034977.4 MB1a month agoMIT
react-intersection-observer05,539164 kB32 months agoMIT
react-visibility-sensor02,322-827 years agoMIT

Feature Comparison: react-in-viewport vs react-intersection-observer vs react-visibility-sensor

API Design

  • react-in-viewport:

    This library provides a higher-order component (HOC) that wraps around your components, making it easy to integrate visibility detection into existing components. It offers a simple API with props to manage visibility states, making it user-friendly for quick implementations.

  • react-intersection-observer:

    Utilizes the native Intersection Observer API, providing a hook-based approach that allows for more granular control over visibility detection. This design promotes a more modern React development style, encouraging the use of hooks for state management and side effects.

  • react-visibility-sensor:

    Offers a component-based API that allows you to wrap any component and receive visibility updates through props. It provides a more traditional approach that may be easier for developers familiar with class-based components.

Performance

  • react-in-viewport:

    While efficient for most use cases, it may not be as optimized as 'react-intersection-observer' since it relies on scroll event listeners, which can lead to performance issues in complex applications with many components being monitored.

  • react-intersection-observer:

    Highly optimized for performance as it leverages the browser's native Intersection Observer API, which is designed to efficiently manage visibility changes without triggering excessive reflows and repaints, making it suitable for performance-critical applications.

  • react-visibility-sensor:

    Performance can vary based on how many components are being monitored simultaneously, as it uses scroll event listeners. It may not be as performant as 'react-intersection-observer' for applications with many visibility checks.

Use Cases

  • react-in-viewport:

    Best suited for applications that require basic visibility detection without complex requirements. Ideal for lazy loading images or triggering animations when components enter the viewport.

  • react-intersection-observer:

    Perfect for applications that need precise control over visibility detection, such as infinite scrolling, lazy loading of large lists, or triggering animations based on the visibility of elements in a more performance-sensitive manner.

  • react-visibility-sensor:

    Great for scenarios where you need to track visibility percentage or require detailed callbacks for visibility changes, such as analytics tracking or conditional rendering based on visibility metrics.

Learning Curve

  • react-in-viewport:

    Easy to learn and implement, especially for developers who are new to React. The HOC pattern is straightforward and requires minimal setup, making it accessible for quick projects.

  • react-intersection-observer:

    Requires a basic understanding of hooks and the Intersection Observer API, which may present a slight learning curve for beginners but is generally straightforward for those familiar with modern React patterns.

  • react-visibility-sensor:

    Relatively easy to use, especially for those accustomed to component-based design in React. The API is intuitive, but understanding the implications of visibility metrics may require additional learning.

Community and Support

  • react-in-viewport:

    Has a moderate community and support, with sufficient documentation and examples available. However, it may not be as widely adopted as some other libraries, which could affect long-term support.

  • react-intersection-observer:

    Benefits from a strong community and is actively maintained, providing good documentation and examples. Its reliance on the native Intersection Observer API also means it is aligned with modern web standards.

  • react-visibility-sensor:

    Has a decent community and support, but may not be as actively maintained as 'react-intersection-observer'. Documentation is available, but users might find fewer resources compared to more popular libraries.

How to Choose: react-in-viewport vs react-intersection-observer vs react-visibility-sensor

  • react-in-viewport:

    Choose 'react-in-viewport' if you need a simple and straightforward solution that provides a higher-order component (HOC) for visibility detection. It's ideal for projects where you want to quickly implement viewport detection without extensive configuration.

  • react-intersection-observer:

    Opt for 'react-intersection-observer' if you prefer a more modern and flexible API that leverages the Intersection Observer API. This package is suitable for performance-sensitive applications, as it minimizes reflows and repaints by using a more efficient approach to detect visibility changes.

  • react-visibility-sensor:

    Select 'react-visibility-sensor' if you require a more comprehensive solution that includes additional features like visibility percentage and callbacks for visibility changes. This library is great for scenarios where you need detailed visibility metrics and want to trigger specific actions based on the visibility state.

README for react-in-viewport

React In Viewport

npm gzip size downloads


Library to detect whether or not a component is in the viewport, using the Intersection Observer API.

This library also uses MutationObserver to detect the change of the target element.

npm install --save react-in-viewport

yarn add react-in-viewport

Examples

Demo

Why

A common use case is to load an image when a component is in the viewport (lazy load).

We have traditionally needed to monitor scroll position and calculate the viewport size, which can be a scroll performance bottleneck.

Modern browsers now provide a new API--Intersection Observer API--which can make implementating this effort much easier and performant.

Polyfill

For browsers not supporting the API, you will need to load a polyfill. Browser support table

require('intersection-observer');

Design

The core logic is written using React Hooks. We provide two interfaces: you can use handleViewport, a higher order component (HOC) for class based components, or use hooks directly, for functional components.

The HOC acts as a wrapper and attaches the intersection observer to your target component. The HOC will then pass down extra props, indicating viewport information and executing a callback function when the component enters and leaves the viewport.

Usages

Using Higher Order Component

When wrapping your component with handleViewport HOC, you will receive inViewport props indicating whether the component is in the viewport or not.

handleViewport HOC accepts three params: handleViewport(Component, Options, Config)

ParamsTypeDescription
ComponentReact ElementCallback function for when the component enters the viewport
OptionsObjectOptions you want to pass to Intersection Observer API
ConfigObjectConfigs for HOC (see below)

Supported config

ParamsTypeDefaultDescription
disconnectOnLeavebooleanfalseDisconnect intersection observer after leave

HOC Component Props

PropsTypeDefaultDescription
onEnterViewportfunctionCallback function for when the component enters the viewport
onLeaveViewportfunctionCallback function for when the component leaves the viewport

The HOC preserves onEnterViewport and onLeaveViewport props as a callback

Props passed down by HOC to your component

PropsTypeDefaultDescription
inViewportbooleanfalseWhether your component is in the viewport
forwardedRefReact refAssign this prop as a ref on your component
enterCountnumberNumbers of times your component has entered the viewport
leaveCountnumberNumber of times your component has left the viewport

NOTE: Need to add ref={this.props.forwardedRef} to your component

Example of a functional component

import handleViewport, { type InjectedViewportProps } from 'react-in-viewport';

const Block = (props: InjectedViewportProps<HTMLDivElement>) => {
  const { inViewport, forwardedRef } = props;
  const color = inViewport ? '#217ac0' : '#ff9800';
  const text = inViewport ? 'In viewport' : 'Not in viewport';
  return (
    <div className="viewport-block" ref={forwardedRef}>
      <h3>{ text }</h3>
      <div style={{ width: '400px', height: '300px', background: color }} />
    </div>
  );
};

const ViewportBlock = handleViewport(Block, /** options: {}, config: {} **/);

const Component = (props) => (
  <div>
    <div style={{ height: '100vh' }}>
      <h2>Scroll down to make component in viewport</h2>
    </div>
    <ViewportBlock onEnterViewport={() => console.log('enter')} onLeaveViewport={() => console.log('leave')} />
  </div>
))

Example for enter/leave counts

  • If you need to know how many times the component has entered the viewport, use the prop enterCount.
  • If you need to know how many times the component has left the viewport, use the prop leaveCount.
import React, { Component } from 'react';
import handleViewport from 'react-in-viewport';

class MySectionBlock extends Component {
  getStyle() {
    const { inViewport, enterCount } = this.props;
    //Fade in only the first time we enter the viewport
    if (inViewport && enterCount === 1) {
      return { WebkitTransition: 'opacity 0.75s ease-in-out' };
    } else if (!inViewport && enterCount < 1) {
      return { WebkitTransition: 'none', opacity: '0' };
    } else {
      return {};
    }
  }

  render() {
    const { enterCount, leaveCount, forwardedRef } = this.props;
    return (
      <section ref={forwardedRef}>
        <div className="content" style={this.getStyle()}>
          <h1>Hello</h1>
          <p>{`Enter viewport: ${enterCount} times`}</p>
          <p>{`Leave viewport: ${leaveCount} times`}</p>
        </div>
      </section>
    );
  }
}
const MySection = handleViewport(MySectionBlock, { rootMargin: '-1.0px' });

export default MySection;

Using Hooks

Alternatively, you can also directly using useInViewport hook which takes similar configuration as HOC.

import React, { useRef } from 'react';
import { useInViewport } from 'react-in-viewport';

const MySectionBlock = () => {
  const myRef = useRef(null);
  const {
    inViewport,
    enterCount,
    leaveCount,
  } = useInViewport(
    myRef,
    options,
    config = { disconnectOnLeave: false },
    props
  );

  return (
    <section ref={myRef}>
      <div className="content" style={this.getStyle()}>
        <h1>Hello</h1>
        <p>{`Enter viewport: ${enterCount} times`}</p>
        <p>{`Leave viewport: ${leaveCount} times`}</p>
      </div>
    </section>
  );
};

Who is using this component