Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 38 ยท Projects and Practice

Project: Infinite Photo Feed

Build an infinite feed with useInfiniteQuery, cursor pages, an IntersectionObserver sentinel and optimistic likes written into paginated cache pages.

Social feeds combine three hard things: loading page after page, keeping scroll position stable, and updating one item that lives deep inside a cached page. In this project you build a feed of colored cards that loads automatically as you scroll, with a like button that updates instantly.

It reuses ideas from Infinite Queries, Infinite Scroll Patterns and Optimistic Updates in the Cache.

The data shape

The fake API returns pages of five cards and a nextCursor, which is the id of the next card or null at the end. A cursor is more robust than a page number, because inserting a card at the top does not shift later pages.

JavaScript
// GET /feed?cursor=6
{ items: [{ id: 6, title: "Card 6", likes: 2, liked: false }, /* ... */], nextCursor: 11 }

useInfiniteQuery stores { pages: [page1, page2, ...], pageParams: [undefined, 6, ...] }. To like a card you must find it in the right page and replace only that object.

The app

Infinite feed

Scroll down inside the preview: the sentinel enters the viewport and the next page loads. Click Like: the count changes immediately and stays correct even for cards from earlier pages.

How it works

Cursor pagination

getNextPageParam receives the last page and returns the cursor for the next request, or undefined to say there is nothing more. Returning undefined (or null) makes hasNextPage false.

The sentinel

An IntersectionObserver watches a small element after the last card. When it becomes visible, the effect calls fetchNextPage(). Guard with hasNextPage and !isFetchingNextPage so you never fire two requests for the same page.

The like mutation

The updater maps over pages and then items, producing new objects only along the path to the changed card. Other objects stay referentially equal, so unrelated cards do not re-render. We skip onSettled invalidation on purpose: refetching an infinite query refetches every loaded page one after another, which is expensive and can reshuffle a feed. If you cannot trust the optimistic value, invalidate with refetchType: "none" so pages refetch only the next time they are used.

flowchart LR
  A["Click like"] --> B["onMutate: edit pages[n].items[k]"]
  B --> C["Server request"]
  C -->|ok| D["Keep cache as is"]
  C -->|error| E["Restore snapshot"]

The optimistic edit is the final state on success, so no refetch is needed.

Limits and polish

  • maxPages. For endless feeds set maxPages: 5 together with both getNextPageParam and getPreviousPageParam, so memory stays bounded.
  • Refetch behavior. When the window regains focus, the whole feed refetches if stale. Raise staleTime for feeds where freshness matters less.
  • Scroll restoration. The cache keeps all pages, so returning to the feed renders everything at once, but restoring the scroll offset is your job (store window.scrollY before leaving).
  • Duplicates. If new cards can be inserted at the top, offset-based APIs can repeat items across pages. Cursors avoid that; de-duplicate by id if you cannot.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What tells useInfiniteQuery there are no more pages?

    When getNextPageParam returns undefined (or null), hasNextPage becomes false.
  2. 2. Why does the like mutation map over both pages and items?

    You must rebuild only the page and item that changed, keeping other references the same.
  3. 3. Why does the project skip invalidateQueries after the like?

    Refetching an infinite query refetches all its pages sequentially, which is costly and unnecessary when the optimistic edit matches the server.
  4. 4. Why guard fetchNextPage with !isFetchingNextPage?

    The observer can fire repeatedly; without the guard you could request the same cursor twice.

Summary

  • Cursor-based getNextPageParam is more stable than page numbers.
  • An IntersectionObserver sentinel loads pages automatically; guard against duplicate fetches.
  • Update items inside pages[n].items immutably with setQueryData.
  • Avoid refetching all pages after a mutation when an optimistic edit is enough.
  • Use maxPages and a longer staleTime for long-lived feeds.

Next: Project: Live Dashboard