0Pricing
React Academy · Lesson

SWR vs React Query: Choosing the Right Tool

Compare SWR and React Query across API surface, bundle size, and use case fit.

SWR vs React Query: Choosing the Right Tool is a free React Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Bundle Size Comparison

SWR weighs approximately 7kb gzipped. React Query v5 is approximately 13kb gzipped. The size difference reflects React Query's additional features: query prefetching, query cancellation, a more detailed query lifecycle, and built-in DevTools.

For size-sensitive applications or simple fetch-display patterns, SWR's smaller footprint is a meaningful advantage.

SWR Strengths

SWR has a simpler, more minimalistic API. If your primary need is fetching data and displaying it with automatic revalidation, SWR covers this elegantly with very little boilerplate.

Being built by Vercel, SWR integrates exceptionally well with Next.js, especially for combining getServerSideProps or getStaticProps prefetch with client-side revalidation via fallback data.

SWR and Next.js Integration

Pass prefetched data to SWR via the fallback option in SWRConfig or the fallbackData option in useSWR. SWR uses this as the initial cache value, eliminating the first-load flash while enabling background revalidation.

This pattern is the recommended data fetching approach for Next.js Pages Router applications with Vercel.

React Query Strengths

React Query offers finer control over the query lifecycle: staleTime, gcTime (garbage collection), refetchOnWindowFocus, retry count, and retry delay are all individually configurable. Its mutation system with onSuccess, onError, and onSettled callbacks is expressive.

React Query also supports query cancellation via AbortSignal, useful for search-as-you-type UIs.

Infinite Queries

React Query's useInfiniteQuery with getNextPageParam is purpose-built for infinite scroll and load-more patterns. It manages the page cursor logic and provides a flat or structured pages object.

SWR has useSWRInfinite which covers similar ground but with a slightly lower-level API requiring more manual page assembly.

Query Prefetching

React Query supports prefetching queries on hover or on route change with queryClient.prefetchQuery. Data is in the cache before the component mounts, giving an instant render experience.

SWR does not have a built-in prefetch API beyond SSR fallback data. For hover-based prefetching, React Query is the better choice.

React Query v5 Improvements

React Query v5 simplified the API by making useQuery return isPending instead of isLoading, unifying optimistic update patterns, and improving TypeScript inference. The v5 API is cleaner and more consistent than earlier versions.

If evaluating React Query, target v5 directly rather than learning v4 and migrating.

Automatic Global Cache Sharing

SWR shares cache entries globally across components by key without any provider configuration beyond SWRConfig for defaults. React Query requires QueryClientProvider with a QueryClient instance.

For rapid prototyping or simple apps, SWR's zero-configuration global cache is faster to set up. React Query's explicit client is better for large apps with complex caching requirements.

When to Choose SWR

Choose SWR when your app is primarily built on Next.js, the data fetching patterns are straightforward (fetch-display-revalidate), you want minimal bundle size, or your team values API simplicity over advanced features.

SWR is also a great choice when adopting an incremental data fetching library into an existing codebase without a major migration.

When to Choose React Query

Choose React Query when you need complex server state management: infinite scroll, query prefetching, background refetch strategies, multi-step query dependencies, or fine-grained cache TTL control.

React Query also has a more active ecosystem of middleware and DevTools, and its queryClient can be tested more easily in isolation.

The Hybrid Approach

Some teams use both: SWR for simple single-resource fetches in leaf components, and React Query for complex multi-query pages with prefetching. This is uncommon but valid if the team is proficient in both APIs.

More commonly, teams pick one and apply it consistently throughout the application to avoid context switching and dual dependency overhead.

SWR vs React Query Bundle Size

Approximately how do the bundle sizes of SWR and React Query compare?

Lesson Recap

SWR (~7kb) is simpler, integrates natively with Next.js, and covers most fetch-display patterns with minimal code. React Query (~13kb) offers richer features: infinite queries, prefetching, fine-grained lifecycle callbacks, and query cancellation.

Choose SWR for simplicity and Next.js projects; choose React Query for complex server state requirements. Both support TypeScript, Suspense, SSR, and offline patterns.

Frequently asked questions

Is the “SWR vs React Query: Choosing the Right Tool” lesson free?

Yes — the full text of “SWR vs React Query: Choosing the Right Tool” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.

What will I learn in “SWR vs React Query: Choosing the Right Tool”?

Compare SWR and React Query across API surface, bundle size, and use case fit. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start React Academy?

No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “SWR vs React Query: Choosing the Right Tool” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this React Academy lesson?

Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. SWR Core Concepts: Stale-While-Revalidate
  2. useSWR Hook: Fetching, Loading, and Error States
  3. Mutation and Optimistic Updates with SWR
  4. SWR vs React Query: Choosing the Right Tool
← Back to React Academy