Open History to see the last commit
1# TanStack Preact 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)`, a `queryOptions` factory keyed by `['characters', 'detail', id]` with a 1-minute `staleTime`.
8- `src/services/characters/api.ts` does not fetch in `getCharacter`. Instead, it adds the id to an `AsyncBatcher` from `@tanstack/pacer` with `wait: 0`, which sends one request to the Rick and Morty API for all ids queued in the same tick and resolves each query's promise with its character.
9- In `src/index.tsx`, `Characters` calls `useQueries` with one `charactersQueries.detail(id)` per entered id, so each id still has its own cache entry and status.
10β
11To run this example:
12β
13- `npm install`
14- `npm run dev`