Search, lists and data · mid
Infinite scroll with IntersectionObserver
Infinite scroll puts a sentinel element after the last item, observes it with IntersectionObserver (with a rootMargin so loading starts early), loads the next page only when not already loading and more pages exist, dedupes items by id, and cleans up the observer.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Sentinel: an empty div after the list; new IntersectionObserver(cb, { root: null or the scroll container, rootMargin: '300px' }); load when entry.isIntersecting.
- Guards: if (isLoading || !hasMore) return; keep the latest cursor and loading flag in refs (or a pager object) so the callback never reads stale values.
- Merge pages by id (dedupe) — live data shifts pages.
- Cleanup: observer.disconnect() in the effect cleanup; a callback ref is a neat way to observe the sentinel when it mounts.
- States: a loader row, an error row with Retry (don't auto-retry forever), and an end message ('You're all caught up').
- Reset everything when filters/search change; abort the in-flight request.
- Accessibility: the APG feed pattern (role='feed', aria-busy while loading) or a visible 'Load more' button; keyboard users must still reach the footer.
- Many items → combine with virtualisation; restore scroll position on Back.
- Hook shape asked in rounds (Mercor, Adobe): useInfiniteScroll({ fetchPage, hasMore }) returning { items, sentinelRef, status, retry }.
The red pen
Common traps: mistake → fix
Mistake: A scroll listener that checks position on every event
Fix: heavy and easy to get wrong; IntersectionObserver only fires on threshold crossings.
Mistake: No loading guard
Fix: the sentinel fires twice and the same page loads twice.
Mistake: Read page/cursor from a stale closure in the observer callback
Fix: it keeps loading page 2.
Mistake: Forget disconnect
Fix: observers pile up across renders.
Easy to hard
Questions interviewers ask, easy to hard
- Build an infinite-scrolling feed over this paginated API.
- Why IntersectionObserver instead of a scroll listener?
- The same page loads twice. Why? Fix it.
- The list reaches 100,000 items. What now?
- What about keyboard and screen-reader users?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.