recommend · verified 2026-09-08
fetching and caching server data in a React app that already uses server components
pick TanStack Query 5.102.8
Start with server components and no client cache at all: fetch in the server component, mutate in a server action, and let the framework revalidate. Add TanStack Query 5.102.8 only for the screens that need polling, infinite scroll, optimistic updates, or offline retry, and keep the rest on the server.
alternative · SWR
SWR 2.5.1 is smaller and its stale-while-revalidate defaults cover read-mostly dashboards with less API to learn.
Wins when you only need cached reads with revalidate-on-focus and no mutation machinery.
traps for TanStack Query (2)
| severity | versions | symptom and fix |
|---|---|---|
| major | >=5.0.0 | Code ported from v4 breaks: onSuccess on useQuery does nothing, cacheTime is unknown, isLoading changed meaning. v5 takes a single object argument everywhere, renames `cacheTime` to `gcTime`, `useErrorBoundary` to `throwOnError`, and `keepPreviousData` to `placeholderData`, removes the query-level `onSuccess`, `onError` and `onSettled` callbacks, and renames the pending status so `isPending` is what `isLoading` used to mean. The new `isLoading` is `isPending && isFetching`. evidence |
| minor | >=5.0.0 | An infinite query throws at runtime because initialPageParam is missing. v5 made `initialPageParam` required on `useInfiniteQuery` instead of passing `undefined` to the first `queryFn` call. Pass the first page cursor explicitly, and note that server-side queries now default to zero retries, so a flaky upstream fails the render rather than retrying. evidence |
what your agent receives
VERDICT fetching and caching server data in a React app that already uses server components PICK TanStack Query 5.102.8: Start with server components and no client cache at all: fetch in the server component, mutate in a server action, and let the framework revalidate. Add TanStack Query 5.102.8 only for the screens that need polling, infinite scroll, optimistic updates, or offline retry, and keep the rest on the server. ALT SWR: SWR 2.5.1 is smaller and its stale-while-revalidate defaults cover read-mostly dashboards with less API to learn.; wins when you only need cached reads with revalidate-on-focus and no mutation machinery TRAPS (2) [c6b2d461] major: Code ported from v4 breaks: onSuccess on useQuery does nothing, cacheTime is unknown, isLoading changed meaning. [c133c4ac] minor: An infinite query throws at runtime because initialPageParam is missing. VERIFIED 2026-09-08 by wellworn reviewer · recheck 2027-01-06 · sources 3