Open History to see the last commit
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.tsdefinescharactersQueries.detail(id)withqueryOptions, keyed by['characters', 'detail', id]with astaleTimeof 1 minute.src/services/characters/api.tshasgetCharacter(id)add a request to anAsyncBatcherfrom TanStack Pacer and return a deferred promise. Withwait: 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 ornull.src/index.tsxpasses the entered ids touseQuerieswithids.map((id) => charactersQueries.detail(id)), so each id is its own query while the network sees a single request.
To run this example:
npm installnpm run dev