Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

README.md

14 lines904 Bytes

1# TanStack React Query Batching Example
2​
3Several components request characters at the same time, and an async batcher from TanStack Pacer combines those requests into one.
4​
5## How it works
6​
7- `src/services/characters/queries.ts` defines `charactersQueries.detail(id)` with `queryOptions`, keyed by `['characters', 'detail', id]` with a `staleTime` of 1 minute.
8- `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`.
9- `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.
10​
11To run this example:
12​
13- `npm install`
14- `npm run dev`