How To Implement Infinite Scroll in Next - Js With Intersection Observer
How To Implement Infinite Scroll in Next - Js With Intersection Observer
By Divine Orji
Creators and developers continually come up with new ways to enhance apps and provide
value to users.
https://www.freecodecamp.org/news/how-to-implement-infinite-scroll-in-next-js/ 1/9
12/1/24, 12:32 PM How to Implement Infinite Scroll in Next.js with Intersection Observer
One feature that's useful for social media and ecommerce apps in particular is infinite scroll.
It provides a seamless and intuitive browsing experience by reducing the time it takes to see
new content.
This article will teach you how to implement infinite scroll in a Next.js app using JavaScript’s
Intersection Observer API.
For example, suppose your website has many items to display (such as a news feed or
product listing). This technique eliminates the need to click through many pages, allowing
users to explore and discover new content easily.
It also helps to improve the user experience by reducing the time it takes to load new pages
since the content loads dynamically as the user scrolls.
You can implement it in your project using JavaScript’s Intersection Observer API.
https://www.freecodecamp.org/news/how-to-implement-infinite-scroll-in-next-js/ 2/9
12/1/24, 12:32 PM How to Implement Infinite Scroll in Next.js with Intersection Observer
It works for use cases like lazy-loading, smooth animation transition, and infinite scrolling.
You can use this API to detect when certain elements appear on your screen and trigger a
function to make changes to the app.
Prerequisites
To smoothly follow along, you will need these first:
https://www.freecodecamp.org/news/how-to-implement-infinite-scroll-in-next-js/ 3/9
12/1/24, 12:32 PM How to Implement Infinite Scroll in Next.js with Intersection Observer
After generating the repo, copy its Git URL so you can clone it to your PC:
In your PC’s terminal, run the command below to clone your repo:
After successfully cloning the project, install dependencies by running the command below in
your project’s terminal:
https://www.freecodecamp.org/news/how-to-implement-infinite-scroll-in-next-js/ 4/9
12/1/24, 12:32 PM How to Implement Infinite Scroll in Next.js with Intersection Observer
yarn
On completion, run yarn dev in your project’s terminal, and navigate to localhost:3000 on
your browser to see the starter UI:
On successful login, click “Your apps” and “New Application”. Accept the terms of the
agreement, then fill out your app’s name and description:
https://www.freecodecamp.org/news/how-to-implement-infinite-scroll-in-next-js/ 5/9
12/1/24, 12:32 PM How to Implement Infinite Scroll in Next.js with Intersection Observer
After successfully creating your app on Unsplash, scroll down to copy your Access Key.
Open your project in your preferred code editor and create a .env.local file in its root folder
to store your Access Key:
NEXT_PUBLIC_UNSPLASH=yourUnsplashApiAccessKey
In your code editor, navigate to pages/index.js and update the fetchImages function with
the code below:
https://www.freecodecamp.org/news/how-to-implement-infinite-scroll-in-next-js/ 6/9
12/1/24, 12:32 PM How to Implement Infinite Scroll in Next.js with Intersection Observer
In your fetch function, the options object includes a headers property that specifies an
Authorization header with a value of Client-ID
${process.env.NEXT_PUBLIC_UNSPLASH}, which is your API Access Key.
You then parsed your response as JSON and destructured the results property.
Finally, you updated your images state by concatenating the previous value of images
with the results array.
In your pages/index.js file, replace // useEffect here with the code below:
useEffect(() => {
fetchImages();
}, [page]);
Here, you set up useEffect to trigger the fetchImages function whenever page updates.
useEffect(() => {
if (!cardRef?.current) return;
observer.observe(cardRef.current);
}, [isLast]);
Here, you set up an Intersection Observer with the useEffect hook. It detects when each
Card component becomes visible in the viewport.
When a Card component contains the last element in your images array and is visible
(entry.isIntersecting), the Intersection Observer API triggers the newLimit function and
stops observing the target element.
The useEffect hook will run whenever the isLast variable changes.
Update the props in your card component to contain newLimit and isLast:
https://www.freecodecamp.org/news/how-to-implement-infinite-scroll-in-next-js/ 7/9
12/1/24, 12:32 PM How to Implement Infinite Scroll in Next.js with Intersection Observer
<HomeLayout>
{images.map((image, index) => (
<Card
key={image.id}
imgSrc={image.urls.regular}
imgAlt={image.alt_description}
shotBy={image.user.name}
creditUrl={image.links.html}
isLast={index === images.length - 1}
newLimit={() => setPage(page + 1)}
/>
))}
</HomeLayout>;
Here, you mapped through your images array, rendering a list of Card components that each
display the image, the photographer, and the link to the original post on Unsplash.
The isLast prop checks if the current card component is the last one in the images array. It
then triggers the newLimit function from your Intersection Observer API to update the page
count.
https://www.freecodecamp.org/news/how-to-implement-infinite-scroll-in-next-js/ 8/9
12/1/24, 12:32 PM How to Implement Infinite Scroll in Next.js with Intersection Observer
Conclusion
In this article, you learned how to implement infinite scroll in a Next.js app. The capabilities of
Intersection Observer API are not limited to infinite scroll and lazy loading. You will discover
more as you keep practicing. The resources below are a good starting point.
If you read this far, thank the author to show them you care.
Learn to code for free. freeCodeCamp's open source curriculum has helped more than
40,000 people get jobs as developers. Get started
https://www.freecodecamp.org/news/how-to-implement-infinite-scroll-in-next-js/ 9/9