Replies: 1 comment
-
I can only say that The better fix is being explicit about dependencies, either by passing That said, if
Unless your observer uses |
Beta Was this translation helpful? Give feedback.
-
I can only say that The better fix is being explicit about dependencies, either by passing That said, if
Unless your observer uses |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Bug Report
There's a race condition between
useQuery
hooks and directgetQueryData()
calls during cache invalidation, specifically when using polling mechanisms that frequently callinvalidateQueries
. Despite parent components usinguseQuery
with conditional rendering that should prevent child component rendering when data is unavailable, child components usinggetQueryData()
still throw "Right side of assignment cannot be destructured" errors.Steps to reproduce
Context: Polling-based cache invalidation
Our app uses a polling mechanism that calls
invalidateQueries
every 3 seconds:Reproduction steps:
useQuery
with conditional renderinggetQueryData()
directlyinvalidateQueries
every 3 secondsExpected behavior
When a parent component using
useQuery
conditionally renders based on data availability, child components should never be rendered in a state where the same cached data is unavailable viagetQueryData()
. Both should access the same underlying cache consistently.Your minimal, reproducible example
Note: This is an intermittent race condition that occurs in production environments but is difficult to reproduce consistently in isolated examples due to its timing-dependent nature.
Production Context:
Stackblitz Demo: https://stackblitz.com/edit/tanstack-query-um5nzlel?file=src%2Findex.jsx
This demo shows the architectural pattern that causes the issue, but may not consistently reproduce the error due to the race condition's timing dependency.
Production Evidence:
getQueryData()
The core issue remains:
useQuery
andgetQueryData()
can return inconsistent values for the same cache key during invalidation cycles, despite both supposedly accessing the same underlying cache.Screenshots or Videos
Production error monitoring shows "TypeError: Right side of assignment cannot be destructured" occurring specifically in components using
getQueryData()
, while parent components usinguseQuery
successfully render with data. The error consistently occurs at line positions where destructuring assignment is attempted on undefined values returned bygetQueryData()
.Platform
TanStack Query information
Additional context
Production Error Analysis
Our error monitoring reveals consistent patterns:
The Race Condition Pattern
The issue occurs when:
invalidateQueries
called → cache marked as stale/invalidateduseQuery
still maintains previous data (React state)getQueryData()
returnsundefined
(cache already invalidated)Key Question
Is this expected behavior where
useQuery
andgetQueryData()
can return different values for the same cache key during invalidation? The documentation suggests they should access the same underlying cache, but our production data shows clear timing inconsistencies affecting thousands of users.Current Workaround: Replace all
getQueryData()
calls withuseQuery
hooks, but this feels like it shouldn't be necessary given both should access the same cache.Root Cause Hypothesis: The issue appears to be that
useQuery
maintains React state through QueryObserver subscriptions whilegetQueryData()
directly accesses cache, creating a window where they're out of sync during invalidation + refetch cycles.Beta Was this translation helpful? Give feedback.
All reactions