Client-side data fetching
When a Client Component should fetch for itself, how use() with a server-started promise compares with SWR and TanStack Query, and how to seed a library.
Client-side data fetching
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Most data should be fetched in Server Components; fetch in the browser when data depends on live interaction, needs polling or focus refresh, or is shared by many client components.
- If a Client Component only needs to read server data once, start the request in a Server Component without await and pass the promise down, then read it with use() inside Suspense.
- SWR and TanStack Query add a browser cache with deduplication, revalidation on focus, polling, and mutations, which the use() pattern does not have.
- A library can be given initial data from the server (SWR's fallback, TanStack Query's HydrationBoundary) so the first paint has content instead of a spinner.
- A client library cache is a separate layer from the Next.js server and client caches, so after a mutation you must invalidate each layer that holds the data.
Why fetch in the browser at all
The default for this roadmap has been: fetch on the server. It is faster, there is nothing for the browser to download, and credentials stay private. So when is the browser the right place?
When the data is a response to something that happens in the browser. Autocomplete is the classic: the query does not exist until the user types. Others are data that must stay live (polling a dashboard, refreshing when the tab regains focus) and data that several client components share and update.
For everything else, a Server Component is simpler.
The no-library option: pass a promise, call use()
The current docs list two ways to fetch in a Client Component. The first needs no library.
Start the request on the server and do not await it. Pass the promise down:
// app/blog/page.tsx (Server Component)
import { Suspense } from 'react'
import Posts from '@/app/ui/posts'
export default function Page() {
const posts = getPosts() // not awaited
return (
<Suspense fallback={<div>Loading…</div>}>
<Posts posts={posts} />
</Suspense>
)
}
// app/ui/posts.tsx
'use client'
import { use } from 'react'
export default function Posts({ posts }: { posts: Promise<{ id: string; title: string }[]> }) {
const allPosts = use(posts)
return <ul>{allPosts.map((p) => <li key={p.id}>{p.title}</li>)}</ul>
}
The fallback shows while the promise resolves. The request started on the server, so credentials never reach the browser; the client component just reads the result. You can read about the hook in the use hook. The docs state that this avoids adding a library for data that never revalidates on the client.
The library option
The second way is SWR or TanStack Query. Use one when Client Components need a shared browser cache: request deduplication, revalidation on focus, polling, or optimistic updates across components.
'use client'
import useSWR from 'swr'
const fetcher = (url: string) => fetch(url).then((r) => r.json())
export default function Results({ query }: { query: string }) {
const { data, error, isLoading } = useSWR(query ? `/api/search?q=${query}` : null, fetcher)
if (isLoading) return <p>Searching…</p>
if (error) return <p>Something went wrong.</p>
return <ul>{data.map((r: { id: string; name: string }) => <li key={r.id}>{r.name}</li>)}</ul>
}
A null key delays the request until there is input. The endpoint here could be a route handler. For the TanStack Query side see TanStack Query fundamentals; for the plain effect version and its pitfalls, data fetching in effects.
Seeding the library from the server
A client-only fetch waits for hydration and then for a request, so the first paint is a spinner. The docs describe a third pattern: the server provides initial data and the library takes over. SWR does this with <SWRConfig fallback>, TanStack Query with <HydrationBoundary>. The docs say the value can be included in the first render or streamed through Suspense.
Three caches, three invalidations
When you combine these with Cache Components, the docs list three layers that can hold related data: the Next.js server cache (freshness controlled by cacheLife revalidate and expire), the Next.js client cache (stale), and the library’s own cache. They have independent freshness policies, and they do not need to match.
The consequence is for mutations. Update the browser value in the library, and invalidate the server read with updateTag or revalidateTag, or the next server render can bring back the old value. If you forget one layer, you see “stale data” bugs that look random. The caching, invalidation and mutations lesson in the React roadmap covers the library half.
What the video covers
Cosden Solutions’ video is about React Query with Server Components. By its chapters: setting up the query client, server-side setup, prefetching data, hydrating it on the client, understanding hydration, React Query versus server components, avoiding query fetching on the server, and an infinite-query case. It assumes you already know basic React Query, so the plain useEffect fetch and the use() pattern are covered above and in the runner-up links instead. It was published in February 2025.
Where this fits
That finishes reading data. Everything so far has moved data from the server to the screen; now it flows back. Next: Server Actions basics.
Check yourself
When is it better to fetch in a Client Component than in a Server Component?
When the data depends on live user input, such as autocomplete, needs polling or revalidation on focus, or must be shared and updated across many client components, since those behaviors live in the browser.
How do you stream server data into a Client Component without a library?
Call the data function in a Server Component without await, wrap the Client Component in Suspense, pass the promise as a prop, and read it with use() in the Client Component.
Why might a page that uses a client library plus Cache Components show stale data after a mutation?
Three separate caches can hold the data, the Next.js server cache, the Next.js client cache, and the library's cache, and each needs its own invalidation, for example updateTag on the server and a mutate or invalidate call in the library.
What does seeding SWR or TanStack Query from a Server Component buy you?
The first render already has data, so there is no empty loading state after hydration, and the library keeps managing refreshes in the browser from there.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.