Skip to content

Companion repository for the blog post "Implementing infinite scroll in React". Sample app which fetches images from Unsplash API and displays them with infinite scroll.

Notifications You must be signed in to change notification settings

noiredist/react-pagination-infinite-scroll

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pagination & Infinite Scroll in React

Link to associated blog post

This is a demo React app that simply calls the Unsplash API to get random images, 15 at a time, and displays them.

When you have to to present large number of data records in your React app, you have two options:

Pagination Infinite Scroll

Pagination

You can either have pagination, which means that you'll display certain number of records on a screen on one page, and then the user has to click on a button to load the next set of data.

You can find the implementation of Pagination at src/PaginationApp.js.

Visit this link for a live demo of pagination.

Infinite Scroll

The second option is to use infinite scroll, which means that the user just has to scroll, and it will automatically display the next set of data as the user scrolls further down, similar to how Twitter or Facebook feeds work. It's a much better experience rather than the user having to click on a button every time they want to load next page's data.

There are two ways we can implement Infinite Scroll:

  1. Using Scroll Events Listener: We will listen to DOM Scroll Events emitted and then see if we have reached bottom. If yes, we'll trigger the request. Pretty straight forward. Using scroll events works, but it might cause performance issues in your app because the scroll events listener code runs on the main thread.
  2. Using Intersection Observer: This one is a relatively new approach. Intersection Observer API lets our code register a callback function that is executed whenever an element they wish to monitor enters or exits another element. This way, our webapp no longer needs to do anything on the main thread to watch for this kind of element intersection, which makes it more performant.

You can find the implementations of Infinite scroll using both Scroll Events Listener and Intersection Observer at src/ InfiniteScrollApp.js and src/InfiniteScrollIntersectionObserverApp.js respectively.

Visit this link for a live demo of infinite scroll.

Do check out the blog post for more details.

Note: To run the project locally, you need to create a free account and get an API key from Unsplash and enter it in the .env file. If you want to check how the JSON response from Unsplash looks like, you can find a sample at src/sample_api_response.json.

Cheers!

About

Companion repository for the blog post "Implementing infinite scroll in React". Sample app which fetches images from Unsplash API and displays them with infinite scroll.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 76.9%
  • HTML 11.6%
  • CSS 10.8%
  • Shell 0.7%