Infinite Scrolling

MediumInfinite ScrollReactList30 mins

by Pratik Rai

Read the full write-up

Build an infinitely scrolling feed. Pages of ten items load as the user reaches the bottom, until there are none left.

fetchPage(page) is given: it resolves after 600ms with { items, hasMore }.

Requirements

  1. The first page loads when the component mounts.

  2. Reaching the bottom of the scroll container loads the next page and appends it.

  3. Only one request is ever in flight — scrolling fast must not fire several.

  4. "Loading…" shows while a page is on its way.

  5. When hasMore is false, stop loading and say the feed has ended.

  6. The observer is disconnected when the component unmounts.

Notes

  • The feed scrolls inside its own 380px box, not the page.

  • IntersectionObserver is the intended approach; a scroll handler measuring scrollTop also works but is harder to get right.

Hints

Loading editor…