Skip to content

Commit bbb2244

Browse files
authoredAug 1, 2024
fix(core): query should reset to default state even when created from hydration (#7837)
1 parent c7245c9 commit bbb2244

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed
 

‎packages/query-core/src/__tests__/query.test.tsx

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { afterEach, beforeEach, describe, expect, it, test, vi } from 'vitest'
22
import { waitFor } from '@testing-library/react'
3-
import { QueryObserver, isCancelledError } from '..'
3+
import { QueryObserver, dehydrate, hydrate, isCancelledError } from '..'
44
import {
55
createQueryClient,
66
mockOnlineManagerIsOnline,
@@ -389,6 +389,26 @@ describe('query', () => {
389389
expect(query.state.fetchStatus).toBe('idle') // not be loading any longer
390390
})
391391

392+
test('should reset to default state when created from hydration', async () => {
393+
const client = createQueryClient()
394+
await client.prefetchQuery({
395+
queryKey: ['string'],
396+
queryFn: () => Promise.resolve('string'),
397+
})
398+
399+
const dehydrated = dehydrate(client)
400+
401+
const hydrationClient = createQueryClient()
402+
hydrate(hydrationClient, dehydrated)
403+
404+
expect(hydrationClient.getQueryData(['string'])).toBe('string')
405+
406+
const query = hydrationClient.getQueryCache().find({ queryKey: ['string'] })
407+
query?.reset()
408+
409+
expect(hydrationClient.getQueryData(['string'])).toBe(undefined)
410+
})
411+
392412
test('should be able to refetch a cancelled query', async () => {
393413
const key = queryKey()
394414

‎packages/query-core/src/query.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ export class Query<
182182
this.#cache = config.cache
183183
this.queryKey = config.queryKey
184184
this.queryHash = config.queryHash
185-
this.#initialState = config.state || getDefaultState(this.options)
186-
this.state = this.#initialState
185+
this.#initialState = getDefaultState(this.options)
186+
this.state = config.state ?? this.#initialState
187187
this.scheduleGc()
188188
}
189189
get meta(): QueryMeta | undefined {

0 commit comments

Comments
 (0)