This website uses cookies to ensure you get the best experience on our website.

Skip to content
LOGO
  • Company

    About IGNEK

    Learn about our story and our mission.

    Careers

    We're hiring!

    We always looking for talented peoples. Join our team!

    Blogs

    The latest industry news, updates and info.

    Case Studies

    Take a closer look at the projects we've delivered

    Webinar

    Watch our latest organized webinar

    Get in touch with our HR team

    • hr@ignek.com
    • +91 93284 95160
    • Ahmedabad, Gujarat, India – 382470
  • Services

    Enterprise Portal Development

    Custom Enterprise Portal Development for flawless Solutions

    Dedicated Development Team

    Expand Teams, Optimize Development with Our Expertise

    Back-end Development

    Empowering your digital presence with robust backend development expertise

    Front-end Development

    Crafting engaging user experiences through expert frontend development

    Digital Transformation

    Embrace digital transformation by modernizing process

    SaaS Development

    Transform your business with SaaS Innovative Solutions

    Technologies

    Liferay Services

    Development & Customization

    Theme

    Upgradation

    Migration

    Proof of Concept

    Performance Tunning

    Architecture

    Support & Maintenance

    E-commerce

    Expert Advice

    React Services

    Enterprise Development

    Custom Application

    Single Page Application

    Architecture

    API Integration

    Migration

    Consulting

    Maintenance & Support

    Spring Boot Services

    Web Service

    MVC Web Development

    API Integration

    Security

    Migration

    Microservices

    Support & Maintenance

    AEM Services

    Consulting Service

    Site Development

    Migration & Upgradation

    Integration

    Maintenance & Support

    Custom Solutions

    Content Management

    Team Augementation

  • Solution

    Ready for digital excellence

    In our rapidly evolving world, businesses are always on the lookout for fresh ways to improve their operations and connect with their audiences.

    Contact US

    Solutions

    SaaS : Software As A Service

    Transforming industries through cutting edge SaaS solutions.

    Customer Portal : Boost Business Productivity

    Streamline operations and enhance productivity with our Customer Portal solution.

    Liferay Based Intranet Portal

    Internal portal for company communication, collaboration and resources

    Integration

    Matomo Integration with Liferay

    Integrate Matomo to enable user interactions seamlessly.

    Microsoft Teams integration with Liferay

    Integrate to enable seamless collaboration, messaging within your portal.

    Jira Integration With Liferay

    Optimizes business operations by integrating with Jira.

  • Hire Us

    Liferay

    Get expert Liferay developers for seamless portal solutions.

    React JS

    Get expert ReactJS developers for dynamic web solutions.

    Web Developer

    Get custom web solutions from IGNEK's expert developers.

    Spring Boot

    Get top-notch Spring Boot developers for your project success!

    Healthcare

    Get top Healthcare Developers for cutting-edge medical software.

    MERN Stack

    Get expert MERN developers from IGNEK for custom, innovative project solutions.

    Why Hire Developers From IGNEK ?

    • Certified Developer
    • Transparent Communication
    • Flexible Hiring Modals
    • Fully Signed NDA
    • Cost Effective
    • Easy Exit Policy

    Get in touch with our Sales team

    • sales@ignek.com
    • +91 6351576580
    • info@ignek.com
    • Ahmedabad, Gujarat, India – 382470
SCHEDULE CALL
  • COMPANY
    • About
    • Case Studies
    • Blogs
    • Career
    • Webinar
  • SERVICES
    • Enterprise Portal Development
    • Dedicated Development Team
    • Back-end Development
    • Front-end Development
    • Digital Transformation
    • SaaS Development
  • SOLUTION
    • Customer Portal:  Boost Business Productivity
    • SaaS : Software as a Service
    • Liferay Based Employee Intranet Portal
  • TECHNOLOGY
    • Liferay Services
      • Liferay Development and Customization
      • Liferay Theme Development Service
      • Liferay DXP 7.4 Upgrade
      • Liferay Migration
      • Liferay Proof Of Concept
      • Liferay Performance Tuning
      • Liferay Architecture Design Service
      • Liferay Support And Maintenance Service
      • Liferay Ecommerce Development
      • Liferay Expert Advice
    • ReactJS Services
      • ReactJS Enterprise Application Development
      • ReactJS Custom Application Development
      • ReactJS Single Page Application Development (SPA)
      • ReactJS Applications Architecture
      • ReactJS API Integration
      • ReactJS Migration
      • ReactJS Consulting
      • ReactJS Maintenance and Support
    • Spring Boot Services
      • Spring Boot Web Development Service
      • Spring MVC Web Development
      • Spring Boot API Integration Service
      • Spring Boot Security
      • Spring Boot Migration Service
      • Spring Boot Microservices
      • Spring Boot Support & Maintenance Service
    • AEM Development Services
      • AEM Consulting Services
      • AEM Site Development
      • AEM Migration & Upgradation
      • AEM Integration Services
      • AEM Maintenance & Support
      • AEM Content Management
      • Custom AEM Solutions
      • AEM Team Augmentation
  • INTEGRATION
  • HIRE US
    • Hire Liferay Developer
    • Hire ReactJS Developer
    • Hire Spring Boot Developer
    • Hire Healthcare Developer
    • Hire Web Developer
    • Hire MERN Stack Developer
  • CONTACT US
  • Company

    About IGNEK

    Careers

    We're hiring!

    Blogs

    Case Studies

    Webinar

    Get in touch with our HR team

    • hr@ignek.com
    • +91 93284 95160
    • Ahmedabad, Gujarat, India – 382470
  • Services

    Enterprise Portal Development

    Dedicated Development Team

    Back-end Development

    Front-end Development

    Digital Transformation

    SaaS Development

    Technologies

    Liferay Liferay Services
    • Development & Customization
    • Theme
    • Upgradation
    • Migration
    • Proof of Concept
    • Performance Tuning
    • Architecture
    • Support & Maintenance
    • E-commerce
    • Expert Advice
    React React Services
    • Enterprise Development
    • Custom Application
    • Single Page Application
    • Architecture
    • API Integration
    • Migration
    • Consulting
    • Maintenance & Support
    Spring Boot Spring Boot Services
    • Web Service
    • MVC Web Development
    • API Integration
    • Security
    • Migration
    • Microservices
    • Support & Maintenance
    AEM AEM Services
    • Consulting Service
    • Site Development
    • Migration & Upgradation
    • Integration
    • Maintenance & Support
    • Custom Solutions
    • Content Management
    • Team Augmentation
  • Solution

    Ready for digital excellence

    In our rapidly evolving world, businesses are always on the lookout for fresh ways to improve their operations and connect with their audiences.

    Contact US

    Solutions

    SaaS : Software As A Service

    Customer Portal : Boost Business Productivity

    Liferay Based Intranet Portal

    Integration

    Matomo Integration with Liferay

    Microsoft Teams integration with Liferay

    Jira Integration With Liferay

  • Hire Us

    Liferay

    React JS

    Web Developer

    Spring Boot

    Healthcare

    MERN Stack

    Why Hire Developers From IGNEK ?

    • Certified Developer
    • Transparent Communication
    • Flexible Hiring Modals
    • Fully Signed NDA
    • Cost Effective
    • Easy Exit Policy

    Get in touch with our Sales team

    • sales@ignek.com
    • +91 6351576580
    • IGNEK
    • Ahmedabad, Gujarat, India – 382470
  • SCHEDULE CALL

Implement Infinite Scrolling in React by using Intersection Observers API

  • ReactJS
  • March 3, 2025

Share On :

Introduction

Have you ever noticed how apps like Instagram or Twitter show fresh content as you scroll? That’s because of a clever feature called infinite scrolling. It lets you keep browsing without having to click through multiple pages or reload.

Instead of a “Next Page” button, infinite scrolling automatically loads more data as the user scrolls, which creates a smoother, more efficient experience. It’s now used in a lot of apps, from social media to news sites and even online stores.

In this blog, we’ll explore how to build a similar feature with JavaScript. Using the Intersection Observer API, we’ll create a React app that loads posts dynamically as you scroll, just like the content-heavy apps we see today.

Prerequisites
  • Node.js and npm
  • React
  • MUI 
How the Intersection Observer API Works

The Intersection Observer API is a tool that lets us track when an element enters or leaves the viewport. Continuously checking the scroll position is not feasible because it can lead to performance issues. Frequent scroll event listeners trigger multiple updates per second, causing unnecessary re-renders and excessive computations, which can degrade the user experience. So,  instead of checking the scroll position continuously, we can set it up to watch specific elements and run actions when they appear on the screen. In the case of infinite scrolling, it helps us load more content when the last item is visible.

This method is more efficient than older scroll event listeners because it offloads the work to the browser, avoiding performance problems and making the app run smoother.

Create a React application

Start the React App

If you don’t have a React project yet, you can make one like this:

				
					
//Create react app using the following command
npx create-react-app infinite-scrolling

//Install the required library
npm i @mui/material @emotion/react @emotion/styled


				
			

Let’s dive right into the code. We’ll create a React-based app that fetches characters from the Star Wars API (SWAPI) as you scroll. The Star Wars API (SWAPI) is a free, open-source API that provides data from the Star Wars universe, including characters, films, planets, species, and more.

API Endpoint: https://swapi.dev/api

				
					
//App.js

//Necessary Imports

const App = () => {
  const [characters, setCharacters] = useState([]);
  const [page, setPage] = useState(1);
  const [hasMore, setHasMore] = useState(true);
  const [loading, setLoading] = useState(false);
  const observer = useRef(null); 

  // Function to fetch characters from SWAPI
  const fetchCharacters = async () => {
    if (loading) return;
    setLoading(true);
    try {
      const response = await fetch(`https://swapi.dev/api/people/?page=${page}`);
      const data = await response.json();

      if (data?.results?.length) {
        // If there is data, append new results to the existing list 
        setCharacters((prev) => [...prev, ...data.results]);
      } else {
        setHasMore(false);
      }
    } catch (error) {
      console.error("Error fetching data:", error);
      setHasMore(false); 
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    fetchCharacters();
  }, [page]);

  // useCallback to create a ref for the last item
  const lastPostElementRef = useCallback(
    (node) => {
      if (loading) return; 

      if (observer.current) observer.current.disconnect();

      observer.current = new IntersectionObserver((entries) => {
        if (entries[0].isIntersecting && hasMore && !loading) {
          setPage((prevPage) => prevPage + 1); 
        }
      });

      if (node) observer.current.observe(node); 
    },
    [loading, hasMore] 
  );

  return (
    <div>
      <Typography>
        Star Wars Characters
      </Typography>
      <Grid container>
        {characters.map((character, index) => (
          <Grid item key={index}>
            <Card >
              <CardContent>
                <Typography >
                  {character.name}
                </Typography>
                <Typography>
                  Height: {character.height} cm
                </Typography>
                <Typography>
                  Birth Year: {character.birth_year}
                </Typography>
                <Typography>
                  Gender: {character.gender}
                </Typography>
              </CardContent>
            </Card>
          </Grid>
        ))}
      </Grid>
      <div ref={lastPostElementRef}>
        {loading && hasMore ? <CircularProgress /> : <Typography>No more characters to load</Typography>}
      </div>
    </div>
  );
};

export default App;


				
			

Explanation of the Code

1. State Setup

In the beginning, we set up the following state variables using React’s useState hook:

  • const [characters, setCharacters] = useState([]): This stores the fetched characters.
  • page: This keeps track of the current page for fetching more data.
  • hasMore: This tells us if there are more characters to fetch from the API.
  • loading: This indicates whether data is being fetched.

2. Fetching Data

The fetchCharacters function is responsible for making an API call to the Star Wars API (SWAPI) to fetch characters. It uses the current page value to fetch the right set of data.

3. Using the Intersection Observer API

To implement infinite scrolling, we use the Intersection Observer API to detect when the user reaches the bottom of the page. The useCallback hook is used to create a reference (lastPostElementRef) to the last item in the list. This reference is then observed, and when the item becomes visible, it triggers the loading of more content.

Here’s how it works:

  • Creating the lastPostElementRef reference : We create a reference to the last item in the list and attach it using the useCallback hook. This reference is observed by the IntersectionObserver.
  • Assigning the reference : The ref={lastPostElementRef} is assigned to the last element or loading indicator in the list.

When the last item comes into view, it triggers the loading of the next page, creating a seamless infinite scrolling experience.

4. Rendering the Characters

We render the fetched characters inside Material UI’s Card components, displayed in a grid layout. The lastPostElementRef is applied to the last item to detect when it’s in the viewport, triggering the loading of more data.

5. Loading Indicator

We display a loading spinner (CircularProgress) when new data is being fetched. If no more characters are available, we display a message saying “No more characters to load.”

Output
Conclusion

To sum up, infinite scrolling makes browsing smoother by loading content as you scroll. With the Intersection Observer API in React, we’ve built a simple way to load data dynamically, just like in popular apps.

Explore Our Services

Discover how we can help your business thrive, whether you’re running a small startup, an SME, or a large enterprise. We’re here to understand your unique needs and goals, offering the expertise and resources to support your journey to success.
Stay informed about our React services and updates by subscribing to our newsletter—just fill in the details below to subscribe.

Loading
Loading...

Related Blogs

March 27, 2024
Authentication and Authorization in React
September 20, 2024
Building Multi-Step Forms in React
Building Multi-Step Forms in React
November 28, 2024
Building a CRUD Application with React and Spring Boot
Loading...

Featured Projects Portfolios

June 19, 2024
Music License Management Portal _ Onboarding & Data Integrity
Music License Management Portal : Onboarding & Data Integrity
November 25, 2024
Liferay DXP Based Intranet Portal Migration from 7.0 to 7.4
Liferay DXP Based Intranet Portal Migration from 7.0 to 7.4
November 29, 2024
Employee Intranet Portal for Government Entity
Employee Intranet Portal for Government Entity

Digital Solutions for Your Business with IGNEK

4.9

5.0

5.0

5.0

LOGO

Making the world a better place through constructing elegant hierarchies

COMPANY

  • About
  • Career
  • Case Study
  • Blogs

SERVICES

  • Enterprise Portal Development
  • Dedicated Development Team
  • Back-end Development
  • Front-end Development
  • Digital Transformation
  • SaaS Development

HIRE US

  • Liferay
  • Spring Boot
  • ReactJS
  • Healthcare
  • MERN Stack
  • AEM

CONTACT

  • info@ignek.com
  • info@ignek.com
SALES
  • sales@ignek.com
  • (+91) 635 157 6580
CAREER
  • hr@ignek.com
  • (+91) 932 849 5160
  • E 910-912, Ganesh Glory 11, Jagatpur Road, SG Highway, Ahmedabad, Gujarat - 382470

© 2018-2025 IGNEK, Inc. All rights reserved

Linkedin Facebook X-twitter Instagram