0
Fork 0
mirror of https://github.com/TanStack/query.git synced 2026-11-18 19:33:22 +00:00
query/examples/react/batching
Wonsuk Choi 70441f460d
docs(examples): rewrite the READMEs, add the framework or router to example titles, and rename examples to '<feature>-<framework>' (#12583)
* docs(examples): describe each example in its README with a summary, the framework it is built with, how it works and the npm commands

* docs(examples): add the framework or router to the README heading and page title of examples that have variants

* docs(examples/react): rename the Next.js examples to '<feature>-nextjs-<pages|app>-router'

* docs(examples/react): update package names, docs links and labels, and the lockfile for the renamed Next.js examples

* docs(examples): rename 'svelte/sveltekit', 'solid/solid-start-streaming' and 'angular/router' to '<feature>-<framework>'

* docs(examples): correct README details on staleness, hydration, 'hasMore', streaming and dependent queries, and prefetch on 'focusin' in 'vue/prefetching-nuxt4'
History 2026-11-12 16:58:15 +09:00
..
public docs(example): batching (#11008) 2026-08-20 11:25:31 +02:00
src fix(examples): validate ids, handle HTTP and loader errors, and show watchers and unknown locations correctly (#12435) 2026-10-28 16:56:52 +09:00
.gitignore docs(example): batching (#11008) 2026-08-20 11:25:31 +02:00
eslint.config.js chore(examples/*/eslint.config): use 'flat/recommended-strict' from '@tanstack/eslint-plugin-query' (#12385) 2026-10-26 02:55:51 +09:00
index.html docs(examples): unify page titles to 'TanStack <Framework> Query <Name> Example' and remove scaffold leftovers (#12580) 2026-11-12 14:54:59 +09:00
package.json ci: Version Packages (#12543) 2026-11-07 00:00:45 +01:00
README.md docs(examples): rewrite the READMEs, add the framework or router to example titles, and rename examples to '<feature>-<framework>' (#12583) 2026-11-12 16:58:15 +09:00
tsconfig.json chore(examples/*/tsconfig): include '*.config.ts' and '*.config.js' like the packages (#12408) 2026-10-27 09:11:08 +09:00
vite.config.ts docs(example): batching (#11008) 2026-08-20 11:25:31 +02:00

TanStack React Query Batching Example

Several components request characters at the same time, and an async batcher from TanStack Pacer combines those requests into one.

How it works

  • src/services/characters/queries.ts defines charactersQueries.detail(id) with queryOptions, keyed by ['characters', 'detail', id] with a staleTime of 1 minute.
  • src/services/characters/api.ts has getCharacter(id) add a request to an AsyncBatcher from TanStack Pacer and return a deferred promise. With wait: 0, the requests collected in the same tick are sent as one call to /api/character/[ids], and each promise resolves with its own character or null.
  • src/index.tsx passes the entered ids to useQueries with ids.map((id) => charactersQueries.detail(id)), so each id is its own query while the network sees a single request.

To run this example:

  • npm install
  • npm run dev