Quick Answer
SWR is Vercel's data fetching library, while TanStack Query v5 includes devtools, mutation lifecycle, optimistic updates, prefetching, and SSR support with fine-grained control. React Query is now TanStack Query, and its React package is version 5; both libraries still solve client-side server state problems that React 19 does not fully replace.
Introduction
React Query vs SWR is less about picking a winner than matching a server state library to the work your product actually performs. React 19 narrows some gaps with Suspense, use, and useOptimistic, but client-initiated data still needs a way to create and cache promises. The cost of a poor choice appears later, when one successful write must update several views without leaving stale UI behind.
Key Takeaways
SWR keeps read-focused client data flows small and direct.
TanStack Query centralizes mutation lifecycle and cache invalidation work.
React 19 primitives complement rather than replace client caches.

React Query vs SWR: Start With the Architectural Difference
React Query was renamed TanStack Query as the project expanded beyond React, but React teams still commonly use both names. The React package is TanStack Query version 5, while SWR teams should confirm the current major version in SWR documentation before adopting a feature. This is one part of a broader scalable tech stack decision: cache behavior becomes product behavior when users edit, paginate, and revisit data.
SWR favors a narrow hook model
SWR is Vercel's data fetching library. Its model is easy to carry across a small frontend and aligns with Vercel’s description of one hook with a small API surface and almost no bundle cost.
Read path: SWR keeps data fetching close to the component.
Cache key: SWR is React-focused.
Mutation path: Mutations can update cached data through SWR's APIs.
Scope: Cache providers isolate application areas.
TanStack Query models server state explicitly
TanStack Query v5 includes mutation lifecycle and optimistic updates. That distinction pays off when a write affects lists, detail pages, counters, and derived UI, especially in a frontend and backend stack where client behavior must stay aligned with API contracts.
Compare Caching, Mutations, and React 19 Integration
When comparing React data fetching libraries, focus on how they recover from a failed write and how much cache coordination your team must write itself. Both can cache query results and revalidate data, but their mutation abstractions differ materially.
Feature comparison for production React apps
The table isolates the operational differences that matter after the first data-fetching hook ships.
Area | TanStack Query | SWR |
|---|---|---|
Queries and caching | Query keys, stale state, invalidation, and selectors. | Keyed cache with hook-led revalidation. |
Mutations |
| Mutation helpers support targeted cache updates and revalidation. |
Infinite data | Dedicated query patterns for paginated data. | Supports paginated and infinite loading hooks. |
Suspense | Can integrate query reads with Suspense. | Can suspend while data resolves. |
Server rendering | Prefetch, dehydrate, and rehydrate cache state. | Client cache patterns remain hook-centered. |
Framework scope | TanStack supports multiple UI frameworks. | Designed for React applications. |
Learning curve | Broader API and more cache concepts. | Smaller API for common reads. |
The practical dividing line is mutation intensity. TanStack Query’s broader surface requires more concepts upfront, while SWR keeps ordinary reads close to the component that needs them.
For teams rebuilding framework boundaries, the same decision affects Next.js and Remix architecture. Server rendering, routing, and the client cache should form one data ownership model rather than separate layers with competing invalidation rules.
Mutations reveal the real tradeoff
const mutation = useMutation({
mutationFn: saveTodo,
onMutate: async (todo) => {
await queryClient.cancelQueries({ queryKey: ["todos"] });
const previous = queryClient.getQueryData(["todos"]);
queryClient.setQueryData(["todos"], (items) => [...items, todo]);
return { previous };
},
onError: (_, __, context) =>
queryClient.setQueryData(["todos"], context.previous),
onSettled: () => queryClient.invalidateQueries({ queryKey: ["todos"] })
});A team must decide which cache keys change and how each rollback behaves. That is acceptable for a focused screen, but manual coordination grows quickly when related data is spread across the interface.
await mutate(
"/api/todos",
async (items) => [...items, await saveTodo()],
{ optimisticData: [...todos, draft], rollbackOnError: true, revalidate: true }
);Use useSWRMutation when a mutation should remain separate from the read hook, but treat multi-key changes as explicit application logic. This is also where a clean REST versus tRPC contract matters, because cache keys should represent stable resources rather than incidental component state.

React 19 and Server Components Change the Boundary
React 19 reduces client boilerplate, not the need for a cache. The use hook reads a promise, but it does not create or cache that promise; client-side data fetching still needs a framework, library, or custom cache. React’s newer hooks, including use and useOptimistic, reduce patterns that previously required third-party code, as covered in the official use hook docs.[1]
Use server fetching for the first render
Fetch on the server when a Server Component can own the initial read, then pass only serializable props into Client Components. For interactive screens, Next.js documents client-side fetching patterns where cache identities and mutation invalidation must remain coordinated across layers, which makes client-side data fetching an architectural concern rather than a hook choice.[2]
TanStack Query supports server prefetching followed by cache dehydration and client rehydration, with the requirement that server and client render from the same dehydrated state. Its server-rendered queries must be prefetched in a Server Component. SWR has tight integration with Next.js and supports SSR hydration.
Keep React primitives and cache state separate
Use useOptimistic for immediate local interaction feedback, then let SWR or TanStack Query reconcile the remote result. React Server Components also require serializable props because values cross the Flight payload boundary; every prop passed from a Server Component to a Client Component is serialized into that payload, a constraint documented in the React Server Components docs.[3]
Bundle size should be measured in your own application, not guessed from package reputation. Versions, imported features, build tooling, and existing dependencies all change the delivered result, which is why a HTMX versus React discussion should also start with actual interaction requirements.

Conclusion
Pick SWR when the application is primarily reading remote data and the team values a small, hook-first API. Pick TanStack Query when mutations, cache invalidation, paginated views, and inspection tooling are recurring product concerns. For React 19, fetch on the server when possible, reserve client caches for interactive state, and avoid duplicating the same resource across unrelated cache layers. Teams should weigh these decisions across framework and API boundaries.
The same tradeoffs show up one layer over, in how a team structures its API gateway design, where caching and invalidation decisions made on the client have to stay consistent with what the server actually exposes.
Frequently Asked Questions (FAQs)
What is React Query and why use it?
React Query, now called TanStack Query, includes devtools, mutation lifecycle, optimistic updates, prefetching, and SSR support with fine-grained control.
How do you implement caching with TanStack Query?
TanStack Query supports caching queries on the server and hydrating them on the client.
What are the differences between useQuery and useMutation?
TanStack Query v5 includes mutation lifecycle, optimistic updates, and prefetching.
How do you perform mutations with TanStack Query?
Use the useMutation hook with a mutationFn, then handle onMutate for optimistic updates, onError to roll back on failure, and onSettled to invalidate affected query keys so the cache reflects the server's final state.
Is React Query better than native fetch?
React Query is now called TanStack Query.
How does React Query handle stale data?
TanStack Query v5 supports prefetching.


