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

React Visibility Detection Libraries

These libraries are designed to help developers detect when a component enters or leaves the viewport, enabling functionalities such as lazy loading images, triggering animations, or managing resource-intensive tasks based on visibility. They provide various methods to handle visibility changes efficiently, enhancing user experience and performance in React applications.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
react-in-viewport0349110 kB2a year agoMIT
react-intersection-observer05,532164 kB216 days agoMIT
react-visibility-sensor02,323-837 years agoMIT
react-waypoint04,06160.7 kB59-MIT

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

API Simplicity

  • react-in-viewport:

    react-in-viewport offers a simple API that allows developers to wrap components with a higher-order component (HOC) to easily detect visibility. This simplicity makes it accessible for quick implementations without extensive configuration.

  • react-intersection-observer:

    react-intersection-observer provides a more complex API that leverages the Intersection Observer API, allowing for detailed configuration of thresholds and root margins. This complexity can be beneficial for advanced use cases but may require a steeper learning curve.

  • react-visibility-sensor:

    react-visibility-sensor has a straightforward API that allows developers to wrap components and receive visibility updates via props. It strikes a balance between simplicity and functionality, making it easy to implement while offering useful features.

  • react-waypoint:

    react-waypoint features a simple API that triggers callbacks when the user scrolls to a specific point. This makes it easy to implement scroll-based events without needing to manage complex visibility states.

Performance

  • react-in-viewport:

    react-in-viewport is lightweight and performs well for simple visibility detection tasks. However, it may not be as optimized for handling many elements simultaneously compared to libraries that utilize the Intersection Observer API.

  • react-intersection-observer:

    react-intersection-observer is highly performant as it utilizes the native Intersection Observer API, which is optimized for detecting visibility changes without causing layout thrashing, making it suitable for applications with many visible elements.

  • react-visibility-sensor:

    react-visibility-sensor is efficient for detecting visibility but may introduce some overhead in scenarios with many components, as it does not leverage the Intersection Observer API directly.

  • react-waypoint:

    react-waypoint is efficient for triggering events based on scroll positions but may not be as performant as Intersection Observer-based libraries when tracking multiple elements.

Use Cases

  • react-in-viewport:

    Best suited for simple use cases where you need to detect if an element is visible on the screen, such as lazy loading images or triggering animations when elements come into view.

  • react-intersection-observer:

    Ideal for complex scenarios that require precise control over visibility thresholds, such as infinite scrolling, lazy loading multiple components, or tracking visibility for analytics.

  • react-visibility-sensor:

    Great for scenarios where partial visibility is important, such as triggering animations based on how much of an element is visible, making it useful for marketing and ad placements.

  • react-waypoint:

    Perfect for scroll-based interactions, such as triggering animations or loading new content as the user scrolls to specific sections of a page.

Community and Support

  • react-in-viewport:

    react-in-viewport has a smaller community compared to others, which may result in fewer resources and examples available for troubleshooting and implementation.

  • react-intersection-observer:

    react-intersection-observer has a growing community and is widely used, providing ample resources, documentation, and community support for developers.

  • react-visibility-sensor:

    react-visibility-sensor has a moderate community, offering decent support and documentation, but may not have as many resources as the more popular libraries.

  • react-waypoint:

    react-waypoint has a solid community and is well-documented, making it easy to find support and examples for common use cases.

Learning Curve

  • react-in-viewport:

    react-in-viewport is easy to learn and implement, making it a good choice for beginners or those looking for quick visibility detection solutions.

  • react-intersection-observer:

    react-intersection-observer may have a steeper learning curve due to its more complex API and the need to understand the Intersection Observer API, but it offers greater flexibility once mastered.

  • react-visibility-sensor:

    react-visibility-sensor is relatively easy to learn, providing a balance between simplicity and functionality that makes it accessible for most developers.

  • react-waypoint:

    react-waypoint is straightforward to learn, especially for those familiar with React's component-based architecture, making it a good choice for quick implementations.

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

  • react-in-viewport:

    Choose react-in-viewport if you need a simple and straightforward solution for detecting whether a component is in the viewport. It is easy to use and integrates well with existing components without much overhead.

  • react-intersection-observer:

    Opt for react-intersection-observer if you want a more powerful and flexible solution based on the Intersection Observer API. It provides more control over visibility thresholds and is suitable for complex use cases, such as tracking multiple elements' visibility at once.

  • react-visibility-sensor:

    Select react-visibility-sensor if you require a library that offers a straightforward API for visibility detection with additional features like handling partial visibility. It is particularly useful for scenarios where you want to trigger actions based on how much of an element is visible.

  • react-waypoint:

    Use react-waypoint if you need a library focused on triggering events when scrolling to specific points in the viewport. It is ideal for implementing scroll-based animations or lazy loading content as the user scrolls.

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