fervor [>]CODING & CURIOSITY
FERVOR LEARNING SYSTEMTUTORIALS
← React

React / 3 MIN READ

Scroll Events

Scrolling and triggering events

From the original Fervor library. Examples may use older package versions.

Tutorial: Using React Scroll Events with Intersection Observer

Introduction: Welcome to the tutorial on using React scroll events with the Intersection Observer API. In this tutorial, you’ll learn how to use the Intersection Observer to track elements as they come into or out of the viewport during scrolling. This can be helpful for creating dynamic effects and triggering actions when certain elements become visible on the page.

Table of Contents:

  1. Introduction
  2. Installing React and Setting Up a Project
  3. Using Intersection Observer in React
  4. Example: Fading In Elements on Scroll
  5. Conclusion

1. Introduction: Scrolling events in web development are commonly used to trigger animations, load content, or change element styles as the user scrolls through a webpage. The Intersection Observer API simplifies this process by providing an efficient way to observe changes in the visibility of elements relative to the viewport.

2. Installing React and Setting Up a Project: Before we begin, ensure you have Node.js and npm installed. You can create a new React project using the following commands:

npx create-react-app scroll-intersection-demo
cd scroll-intersection-demo
npm start

3. Using Intersection Observer in React: The Intersection Observer API allows you to watch elements and react when they enter or exit the viewport. Here’s how you can use it in a React component:

import React, { useRef, useEffect } from 'react';

function App() {
  const targetRef = useRef(null);

  const handleIntersection = (entries) => {
    entries.forEach((entry) => {
      if (entry.isIntersecting) {
        // Element is in the viewport
        entry.target.classList.add('visible');
      } else {
        // Element is out of the viewport
        entry.target.classList.remove('visible');
      }
    });
  };

  useEffect(() => {
    const options = {
      root: null, // Use the viewport as the root
      rootMargin: '0px',
      threshold: 0.5, // Trigger when at least 50% of the element is visible
    };

    const observer = new IntersectionObserver(handleIntersection, options);

    if (targetRef.current) {
      observer.observe(targetRef.current);
    }

    return () => {
      if (targetRef.current) {
        observer.unobserve(targetRef.current);
      }
    };
  }, []);

  return (
    <div className="container">
      <div ref={targetRef} className="box">
        Scroll to see the effect!
      </div>
    </div>
  );
}

export default App;

4. Example: Fading In Elements on Scroll: In this example, the box element will have the visible class added when it’s at least 50% visible in the viewport. You can define the visible class in your CSS to apply a fading-in effect or any other styling you desire.

5. Conclusion: Using the Intersection Observer API in React enables you to create dynamic and engaging scrolling effects. By observing elements as they enter or exit the viewport, you can trigger animations, load content, or apply styles based on user scrolling behavior. Experiment with different threshold values and CSS animations to achieve the desired visual effects for your web application.

Keep your curiosity going.Explore more React →
287 TUTORIALS · 22 TOPICSREADY