Skip to content

perf(tanstack-query): reuse React hooks for no-options schema calls - #2939

Open
clean99 wants to merge 1 commit into
zenstackhq:devfrom
clean99:contrib/tanstack-client-hooks-cache
Open

clean99 wants to merge 1 commit into
zenstackhq:devfrom
clean99:contrib/tanstack-client-hooks-cache

Conversation

@clean99

@clean99 clean99 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Addresses #2908's React no-options caching path. Calling useClientQueries(schema) during rendering currently rebuilds every model's hooks, even when the generated schema is unchanged.

  • Problem: each call scans the schema and allocates a complete hook dictionary. On 3.10.4, this returns false for an existing generated schema:
    const first = useClientQueries(schema);
    console.log(first === useClientQueries(schema));
    
  • Change: cache the complete model, procedure, and transaction hook set in a schema-keyed WeakMap. No-options callers now share the returned object for that immutable schema. Explicit options, including {}, stay uncached. Provider settings and QueryClient are still read when individual hooks execute. Model indexing and Vue/Svelte caching are outside this change.
  • Verification: pnpm --filter @zenstackhq/tanstack-query exec vitest run test/react/issue-2908.test.ts fails with the source fix removed and passes with it. The regression covers schema/options isolation, provider and QueryClient changes, procedure/transaction hooks, and endpoint overrides. The package build, lint, typecheck, all 68 tests, and changed-file formatting pass.

Summary by CodeRabbit

  • Improvements
    • Reusing the same schema without custom options now reuses its generated React hooks, reducing repeated setup.
    • Hooks continue to use the current provider settings when invoked, including updated endpoints and query clients.
    • Providing custom options or a different schema creates a separate hook set.

@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

useClientQueries now caches generated hook sets by schema object when callers omit options. Tests cover cache reuse, cache bypass when options are provided, and endpoint and query-client behavior when hooks run.

Changes: React Query hook caching

Layer / File(s) Summary
Schema-keyed cache and hook behavior
packages/clients/tanstack-query/src/react.ts, packages/clients/tanstack-query/test/react/issue-2908.test.ts
The no-options path reuses a generated hook set for the same schema object and stores new sets in a WeakMap. Explicit options bypass the cache. Tests cover schema identity, current provider endpoints and query clients, endpoint overrides, and mutation-triggered query invalidations.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Possibly related issues

  • zenstackhq/zenstack#2908 — The change caches hook sets for useClientQueries(schema), addressing the reported repeated hook-closure construction. The issue’s proposed model indexing change and Vue and Svelte clients are not covered.

Suggested reviewers: ymc9

Merge Risk: Minimal · up to a4551

This change reuses generated React hooks when the same schema is passed without options, which reduces per-render rebuild work. No concrete defect was found, and behavior is unchanged for callers that pass options. The change is low risk to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: React hook sets are reused for calls to useClientQueries that pass no options, in the tanstack-query package. It uses the conventional perf(...) prefix. The changeset …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: React hook sets are reused for calls to useClientQueries that pass no options, in the tanstack-query package. It uses the conventional perf(...) prefix. The changeset caches the hook set in a schema-keyed WeakMap and adds regression tests, which matches the title's scope.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/clients/tanstack-query/src/react.ts

ESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox.

packages/clients/tanstack-query/test/react/issue-2908.test.ts

ESLint skipped: the matched ESLint configuration already failed (missing-dependency).

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between e633b97 and a4551e8.

Files selected for processing: 2

  • packages/clients/tanstack-query/src/react.ts
  • packages/clients/tanstack-query/test/react/issue-2908.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

This branch has not been deployed

No deployments
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.

1 participant