Skip to content

feat(fetchium): add useQueries to fetchium/react - #96

Merged
iamacook merged 2 commits into
mainfrom
aaron-c/use-queries
Nov 16, 2026
Merged

iamacook merged 2 commits into
mainfrom
aaron-c/use-queries

Conversation

@iamacook

@iamacook iamacook commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

This adds useQueries to fetchium/react, for a list of queries whose length or contents change between renders. Until now the only way to handle that was to render a child component per query, because calling useQuery in a loop breaks the rules of hooks.

const users = useQueries(ids.map(id => ({ query: GetUser, params: { id } })));
const [user, posts] = useQueries([{ query: GetUser, params: { id } }, { query: GetPosts }]);

Each entry takes the same query, params and enabled as useQuery, and the hook returns one useQuery-shaped result per entry, in order. A fixed list of different queries returns a typed tuple, and each entry's params are checked against its own query.

The hook is small. One memoised thunk maps entries to fetchQuery (or peekQuery for disabled entries), and Signalium's useReactive snapshot already handles arrays of query results, so each entry gets its own structurally shared snapshot. The thunk is keyed on each entry's query, params and enabled, not on the array itself, so passing a new array with the same entries every render keeps every subscription.

Behavior

  • Adding, removing or reordering entries doesn't refetch the ones that stay.
  • A removed entry is released like an unmounted useQuery: it unsubscribes and is collected after gcTime.
  • An entry whose data didn't change keeps its result identity, so memoised children re-render only for the entries that changed.
  • enabled: false and Signal params work per entry, the same as in useQuery.
  • Entries fail independently.

Tests

  • The useQuery and useQuery-stability suites now also run against a single-entry useQueries, so every existing useQuery scenario covers both hooks. The describe.each sits on the existing top-level describe, so those diffs stay small.
  • A new use-queries.test.tsx covers the list-specific behavior: adding, removing, reordering and swapping entries; per-entry params changes and enabled; result identity; failures; shared entities; Signal params; thunk reuse and rebuilds; and compile-time param checks.
  • Each part of the memo key (query id, params, enabled) is covered by a test that fails when that part is removed.

Not included: a combine option and a useSuspenseQueries.

@iamacook iamacook self-assigned this Nov 7, 2026
@iamacook
iamacook requested review from a team and pzuraq as code owners November 7, 2026 09:47
Comment thread docs/src/app/api/fetchium-react/page.md Resolved
@iamacook
iamacook merged commit 703d85f into main Nov 16, 2026
1 check passed
@iamacook
iamacook deleted the aaron-c/use-queries branch November 16, 2026 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants