Skip to content
Source
Sign inSign up
TanStack/query

fix(react-query): hydrate deferred queries before observers subscribe to avoid a redundant refetch #11137

Openostapondo(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/11137/headopened View on GitHub

Showing 3 changed files with 137 additions and 7 deletions.

x
0 of 3 viewed, 3 left
5 additions and 0 deletions.changeset/hungry-planes-tease.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/react-query': patch
+---
+​
+Hydrate deferred queries in a layout effect so a remounting `useQuery` no longer refetches data the dehydrated state already contains.
116 additions and 4 deletionspackages/react-query/src/__tests__/HydrationBoundary.test.tsxModified
View file
@@ -149,14 +149,16 @@
</QueryClientProvider>,
​
- // Existing observer should not have updated at this point,
- // as that would indicate a side effect in the render phase
- expect(rendered.getByText('string')).toBeInTheDocument()
+ // The existing observer has the hydrated data as soon as the tree
+ // commits, the layout effect has already run by this line. It still
+ // doesn't happen during render, that's what the aborted transition
+ // test guards
+ expect(rendered.getByText('should change')).toBeInTheDocument()
// New query data should be available immediately
expect(rendered.getByText('added')).toBeInTheDocument()
​
await vi.advanceTimersByTimeAsync(0)
- // After effects phase has had time to run, the observer should have updated
+ // Nothing changes after the effects phase has had time to run
expect(rendered.queryByText('string')).not.toBeInTheDocument()
expect(rendered.getByText('should change')).toBeInTheDocument()
​