React Waypoint Component

10.3.0 · active · verified Tue Apr 21

React Waypoint is a component designed to trigger functions whenever a specific element scrolls into or out of view within its container, including the window. It is currently at version 10.3.0 and is actively maintained, with regular minor and major releases addressing compatibility and performance. This library is commonly used for implementing features such as lazy loading, infinite scrolling, scrollspies, and elements that dock to the viewport. Its key differentiator is its straightforward React component API for handling scroll intersection events, abstracting away the complexities of `IntersectionObserver` or manual scroll event handling. It offers granular control over triggers with `onEnter`, `onLeave`, and `onPositionChange` callbacks, and supports both vertical and horizontal scrolling with configurable offsets.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `react-waypoint` to create a simple scroll spy, updating the current active section in the UI as the user scrolls.

import React, { useState, useCallback } from 'react';
import { Waypoint } from 'react-waypoint';

const ScrollSpyExample = () => {
  const [currentSection, setCurrentSection] = useState('section1');

  const handleEnter = useCallback((sectionName) => {
    setCurrentSection(sectionName);
    console.log(`Entered ${sectionName}`);
  }, []);

  return (
    <div style={{ height: '300px', overflowY: 'scroll', border: '1px solid gray' }}>
      <p>Scroll down to see Waypoint in action!</p>
      <div style={{ height: '400px', background: '#e0ffe0', padding: '20px' }}>
        <h2>Section 1 (Current: {currentSection === 'section1' ? '✅' : '❌'})</h2>
        <p>This is the content for section 1.</p>
      </div>
      <Waypoint
        onEnter={() => handleEnter('section1')}
        bottomOffset="-100px"
      />

      <div style={{ height: '400px', background: '#ffe0ff', padding: '20px' }}>
        <h2>Section 2 (Current: {currentSection === 'section2' ? '✅' : '❌'})</h2>
        <p>More content for section 2.</p>
      </div>
      <Waypoint
        onEnter={() => handleEnter('section2')}
        bottomOffset="-100px"
      />

      <div style={{ height: '400px', background: '#e0e0ff', padding: '20px' }}>
        <h2>Section 3 (Current: {currentSection === 'section3' ? '✅' : '❌'})</h2>
        <p>Final section content.</p>
      </div>
      <Waypoint
        onEnter={() => handleEnter('section3')}
        bottomOffset="-100px"
      />
    </div>
  );
};

export default ScrollSpyExample;

view raw JSON →