Courses / React 19: From Components to Production / Chapter 16
Module 8 · Server State, Real-Time and Offline · Chapter 16 of 30
Server State with TanStack Query
Builds on Data Fetching: Requests, Loading States, Races, Caching and Errors, Suspense, Streaming SSR and Code-Splitting and Server Components: The Client Boundary, Server Functions and Caching. Next up: Real-Time, Background Work and Offline Support.
Part 1 · Queries, keys and one shared cache
What the library adds to a hand-built cache
A cooperative bank shows each member a balance and a statement, and several components on one screen read the same data. TanStack Query is a library that keeps that server data in one cache, so the screen does not fetch it again for each reader.
The earlier data fetching chapter built a small cache by hand and ended with a table that mapped it onto the library. This chapter does not rebuild the cache. It looks at what the library does that the hand-built version did not: it observes, hashes keys, tunes staleness, coordinates writes, and plugs into Suspense and server rendering.
The pieces are few. A query is a cached result identified by a query key, an array, and produced by a query function that returns a promise. A QueryClient owns the cache. QueryClientProvider makes the client available to the tree, and useQuery subscribes a component to one query. The versions used here are @tanstack/react-query 5.107.1 and its engine @tanstack/query-core 5.107.1. Later releases existed on 22 November 2026, but they were less than two weeks old, so I pinned the newest release that was older than that.