examples/react/prefetching-nextjs-app-router
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack React Query Prefetching Example (Next.js App Router)#
Prefetches a Pokémon in a Server Component and hydrates it in a Client Component with HydrationBoundary.
Built with Next.js (App Router).
How it works#
services/pokemons/queries.tsdefinespokemonsQueries.detail(id)withqueryOptions, andservices/pokemons/api.tsfetches the Pokémon from PokéAPI.app/get-query-client.tscreates a newQueryClientper request on the server and reuses one in the browser. Itsdehydratedefaults include pending queries throughshouldDehydrateQueryand turn offshouldRedactErrors.app/page.tsxis a Server Component that startsqueryClient.query(pokemonsQueries.detail(25))without awaiting it and passesdehydrate(queryClient)toHydrationBoundary, so the pending query is sent to the client.app/pokemon-info.tsxis a Client Component that reads the same options withuseSuspenseQueryand picks up the data streamed from the server instead of fetching again.
To run this example:
npm installnpm run dev