Infinite scrolling has become a popular feature in modern web applications, especially for image galleries, social media feeds, and news articles. It allows users to view more content seamlessly as they scroll down a page, creating a more enjoyable and fluid experience. In this blog post, we will walk through how to implement infinite scrolling in a React application step by step.
Before we dive in, make sure you have the following:
If you don't have a React application set up yet, you can create one quickly with:
npx create-react-app infinite-scroll-example cd infinite-scroll-example
Once you've created the app, navigate to the project folder.
In this example, we'll use Axios to fetch data from a public API. Run the following command to install Axios:
npm install axios
We will create a simple app that loads a list of photos from the JSONPlaceholder API. Here's how to create the necessary components:
PhotoList.js
in the src
folder.PhotoList
component like this:// src/PhotoList.js import React, { useEffect, useState } from 'react'; import axios from 'axios'; const PhotoList = () => { const [photos, setPhotos] = useState([]); const [page, setPage] = useState(1); const [loading, setLoading] = useState(false); // Fetch photos from the API const fetchPhotos = async () => { setLoading(true); const response = await axios.get(`https://jsonplaceholder.typicode.com/photos?_limit=10&_page=${page}`); setPhotos(prevPhotos => [...prevPhotos, ...response.data]); setLoading(false); }; useEffect(() => { fetchPhotos(); }, [page]); // Load more photos when the user scrolls down const handleScroll = () => { const bottom = window.innerHeight + window.scrollY >= document.body.offsetHeight - 200; if (bottom && !loading) { setPage(prevPage => prevPage + 1); } }; useEffect(() => { window.addEventListener('scroll', handleScroll); return () => window.removeEventListener('scroll', handleScroll); }, [loading]); return ( <div> <h1>Infinite Scrolling Photos</h1> <div className="photo-list"> {photos.map(photo => ( <div key={photo.id} className="photo-item"> <img src={photo.thumbnailUrl} alt={photo.title} /> <p>{photo.title}</p> </div> ))} </div> {loading && <p>Loading more photos...</p>} </div> ); }; export default PhotoList;
Now, we need to render the PhotoList
component in our main App.js
file. Open src/App.js
and replace the content with the following:
// src/App.js import React from 'react'; import './App.css'; // Don't forget to add some styles import PhotoList from './PhotoList'; function App() { return ( <div className="App"> <PhotoList /> </div> ); } export default App;
For a better presentation, let's add some simple styles. Open src/App.css
and add the following:
.photo-list { display: flex; flex-wrap: wrap; } .photo-item { margin: 10px; width: 200px; text-align: center; } .photo-item img { width: 100%; } h1 { text-align: center; }
Now that we have everything set up, it's time to run the application.
In your terminal, navigate to your project directory and use:
npm start
This will launch your app in the browser. As you scroll down, you should see more photos loading each time you reach the bottom of the page.
With these steps, you've successfully implemented infinite scrolling in a React application. This approach not only enhances user experience but also keeps your application clean and responsive. Enjoy coding!
14/09/2024 | ReactJS
24/08/2024 | ReactJS
14/09/2024 | ReactJS
22/09/2024 | ReactJS
24/08/2024 | ReactJS
20/09/2024 | ReactJS
13/07/2024 | ReactJS
24/08/2024 | ReactJS
24/08/2024 | ReactJS