You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
perf(tanstack-query): reuse React hooks for no-options schema calls - #2939
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.
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.
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: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
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
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.falsefor an existing generated schema:{}, stay uncached. Provider settings and QueryClient are still read when individual hooks execute. Model indexing and Vue/Svelte caching are outside this change.pnpm --filter @zenstackhq/tanstack-query exec vitest run test/react/issue-2908.test.tsfails 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