fix(query-core): update initialData when an observer mounts while a Query without data exists #9620
initialData gives the guarantee that data cannot be undefined, but this falls short when you create a Query via prefetching, and then mount the observer that has initialData set.
That's because initialData is only doing something in the constructor of the Query, so if the Observer isn't the one who creates the Query, it does nothing
This fix makes sure that when new options are applied on a Query (which e.g. happens when an observer mounts), initialData is taken into account when the Query doesn't have any data yet
Summary by CodeRabbit#
Bug Fixes
- Initial data is now correctly applied when a query exists without data, so observers receive the provided initialData immediately. State transitions remain consistent as network data arrives, with no API changes.
Tests
- Added comprehensive tests covering initialData and updatedAt behavior, fetch status transitions, and reset semantics to ensure reliable query state handling.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: query-core
View your CI Pipeline Execution ↗ for commit a6269e967c09ec51599d628c502960c201c1b958
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 1m 14s View ↗
☁️ Nx Cloud last updated this comment at
2025-09-05 14:02:57UTCMore templates
- @tanstack/query-example-angular-auto-refetching
- @tanstack/query-example-angular-basic
- @tanstack/query-example-angular-basic-persister
- @tanstack/query-example-angular-devtools-panel
- @tanstack/query-example-angular-infinite-query-with-max-pages
- @tanstack/query-example-angular-optimistic-updates
- @tanstack/query-example-angular-pagination
- @tanstack/query-example-angular-query-options-from-a-service
- @tanstack/query-example-angular-router
- @tanstack/query-example-angular-rxjs
- @tanstack/query-example-angular-simple
- @tanstack/query-example-react-algolia
- @tanstack/query-example-react-auto-refetching
- @tanstack/query-example-react-basic
- @tanstack/query-example-react-basic-graphql-request
- @tanstack/query-example-chat
- @tanstack/query-example-react-default-query-function
- @tanstack/query-example-react-devtools-panel
- @tanstack/query-example-eslint-legacy
- @tanstack/query-example-react-infinite-query-with-max-pages
- @tanstack/query-example-react-load-more-infinite-scroll
- @tanstack/query-example-react-nextjs
- @tanstack/query-example-react-nextjs-app-prefetching
- @tanstack/query-example-nextjs-suspense-streaming
- @tanstack/query-example-react-offline
- @tanstack/query-example-react-optimistic-updates-cache
- @tanstack/query-example-react-optimistic-updates-ui
- @tanstack/query-example-react-pagination
- @tanstack/query-example-react-playground
- @tanstack/query-example-react-prefetching
- @tanstack/query-example-react-react-native
- @tanstack/query-example-react-router
- @tanstack/query-example-react-rick-morty
- @tanstack/query-example-react-shadow-dom
- @tanstack/query-example-react-simple
- @tanstack/query-example-react-star-wars
- @tanstack/query-example-react-suspense
- @tanstack/query-example-solid-astro
- @tanstack/query-example-solid-basic
- @tanstack/query-example-solid-basic-graphql-request
- @tanstack/query-example-solid-default-query-function
- @tanstack/query-example-solid-simple
- @tanstack/query-example-solid-start-streaming
- @tanstack/query-example-svelte-auto-refetching
- @tanstack/query-example-svelte-basic
- @tanstack/query-example-svelte-load-more-infinite-scroll
- @tanstack/query-example-svelte-optimistic-updates
- @tanstack/query-example-svelte-playground
- @tanstack/query-example-svelte-simple
- @tanstack/query-example-svelte-ssr
- @tanstack/query-example-svelte-star-wars
- @tanstack/query-example-vue-2.6-basic
- @tanstack/query-example-vue-2.7-basic
- @tanstack/query-example-vue-basic
- @tanstack/query-example-vue-dependent-queries
- @tanstack/query-example-vue-nuxt3
- @tanstack/query-example-vue-persister
- @tanstack/query-example-vue-simple
@tanstack/angular-query-devtools-experimental
npm i https://pkg.pr.new/@tanstack/angular-query-devtools-experimental@9620@tanstack/angular-query-experimental
npm i https://pkg.pr.new/@tanstack/angular-query-experimental@9620@tanstack/eslint-plugin-query
npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@9620@tanstack/query-async-storage-persister
npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@9620@tanstack/query-broadcast-client-experimental
npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@9620@tanstack/query-core
npm i https://pkg.pr.new/@tanstack/query-core@9620@tanstack/query-devtools
npm i https://pkg.pr.new/@tanstack/query-devtools@9620@tanstack/query-persist-client-core
npm i https://pkg.pr.new/@tanstack/query-persist-client-core@9620@tanstack/query-sync-storage-persister
npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@9620@tanstack/react-query
npm i https://pkg.pr.new/@tanstack/react-query@9620@tanstack/react-query-devtools
npm i https://pkg.pr.new/@tanstack/react-query-devtools@9620@tanstack/react-query-next-experimental
npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@9620@tanstack/react-query-persist-client
npm i https://pkg.pr.new/@tanstack/react-query-persist-client@9620@tanstack/solid-query
npm i https://pkg.pr.new/@tanstack/solid-query@9620@tanstack/solid-query-devtools
npm i https://pkg.pr.new/@tanstack/solid-query-devtools@9620@tanstack/solid-query-persist-client
npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@9620@tanstack/svelte-query
npm i https://pkg.pr.new/@tanstack/svelte-query@9620@tanstack/svelte-query-devtools
npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@9620@tanstack/svelte-query-persist-client
npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@9620@tanstack/vue-query
npm i https://pkg.pr.new/@tanstack/vue-query@9620@tanstack/vue-query-devtools
npm i https://pkg.pr.new/@tanstack/vue-query-devtools@9620commit:
a6269e9Actionable comments posted: 0
🧹 Nitpick comments (3)
packages/query-core/src/query.ts (1)
208-219: Minor: clarify guard and avoid unnecessary work- Add an inline reason for the ESLint disable (state can be undefined during constructor’s initial setOptions).
- Micro-optimization: short-circuit when no initialData is provided to avoid computing defaultState.
Apply:
this.updateGcTime(this.options.gcTime) - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - if (this.state && this.state.data === undefined) { + // During construction, `this.state` is undefined; this guard is required. + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + const hasInitial = + this.options.initialData !== undefined || + typeof this.options.initialData === 'function' + if (this.state && this.state.data === undefined && hasInitial) { const defaultState = getDefaultState(this.options) if (defaultState.data !== undefined) { this.setData(defaultState.data, { updatedAt: defaultState.dataUpdatedAt, manual: true, }) this.#initialState = defaultState } }packages/query-core/src/__tests__/query.test.tsx (2)
1196-1256: Use async timer advancement to avoid flakinessSwap to advanceTimersByTimeAsync for deterministic resolution of pending promises, and await both calls.
Apply:
- vi.advanceTimersByTime(50) + await vi.advanceTimersByTimeAsync(50) @@ - vi.advanceTimersByTime(50) + await vi.advanceTimersByTimeAsync(50)
1248-1248: Fix typo in test comment“ot” → “to”.
Apply:
- // resetting should get us back ot 'initialData' + // resetting should get us back to 'initialData'📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📥 Commits
Reviewing files that changed from the base of the PR and between b43af57cd2ccc5b6c3d96c4168e23d1fd811213a and a6269e967c09ec51599d628c502960c201c1b958.
📒 Files selected for processing (2)
packages/query-core/src/__tests__/query.test.tsx(1 hunks)packages/query-core/src/query.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
packages/query-core/src/query.ts (1)
packages/query-core/src/mutation.ts (1)
getDefaultState(349-366)
packages/query-core/src/__tests__/query.test.tsx (2)
packages/query-core/src/utils.ts (1)
sleep(363-367)
packages/query-core/src/query.ts (1)
promise(198-200)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Preview
- GitHub Check: Test
🔇 Additional comments (1)
packages/query-core/src/query.ts (1)
208-219: Initial data seeding on setOptions is correctThis achieves the intended behavior: seeds initialData only when no data exists, preserves fetchStatus via a manual success, and updates #initialState for reset. LGTM.
Codecov Report#
:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 59.62%. Comparing base (
a0b96db) to head (a6269e9). :warning: Report is 2 commits behind head on main.Additional details and impacted files
@@ Coverage Diff @@ ## main #9620 +/- ## =========================================== + Coverage 45.47% 59.62% +14.15% =========================================== Files 209 138 -71 Lines 8372 5615 -2757 Branches 1905 1511 -394 =========================================== - Hits 3807 3348 -459 + Misses 4118 1963 -2155 + Partials 447 304 -143- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
TkDodo merged this1c8a921
TkDodo closed this
Sign in to join this conversation.
Walkthrough#
Introduces logic in Query.setOptions to seed initialData into state when no data exists, and adds a unit test verifying state transitions with initialData, prefetch, resolution, and reset behaviors. No public API signatures changed.
Changes#
packages/query-core/src/query.tspackages/query-core/src/__tests__/query.test.tsxSequence Diagram(s)#
Estimated code review effort#
🎯 3 (Moderate) | ⏱️ ~20 minutes
Suggested labels#
package: query-corePoem#
✨ Finishing Touches
🧪 Generate unit tests
🪧 Tips
Chat#
There are 3 ways to chat with CodeRabbit:
I pushed a fix in commit <commit_id>, please review it.Open a follow-up GitHub issue for this discussion.@coderabbitaiin a new review comment at the desired location with your query.@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.Support#
Need help? Create a ticket on our support page for assistance with any issues or questions.
CodeRabbit Commands (Invoked using PR/Issue comments)#
Type
@coderabbitai helpto get the list of available commands.Other keywords and placeholders#
@coderabbitai ignoreor@coderabbit ignoreanywhere in the PR description to prevent this PR from being reviewed.@coderabbitai summaryto generate the high-level summary at a specific location in the PR description.@coderabbitaianywhere in the PR title to generate the title automatically.CodeRabbit Configuration File (
.coderabbit.yaml)#.coderabbit.yamlfile to the root of your repository.# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.jsonStatus, Documentation and Community#