diff --git a/packages/query-core/src/__tests__/query.test.tsx b/packages/query-core/src/__tests__/query.test.tsx index 701776688a..e61f8664ae 100644 --- a/packages/query-core/src/__tests__/query.test.tsx +++ b/packages/query-core/src/__tests__/query.test.tsx @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it, test, vi } from 'vitest' import { waitFor } from '@testing-library/react' -import { QueryObserver, isCancelledError } from '..' +import { QueryObserver, dehydrate, hydrate, isCancelledError } from '..' import { createQueryClient, mockOnlineManagerIsOnline, @@ -389,6 +389,26 @@ describe('query', () => { expect(query.state.fetchStatus).toBe('idle') // not be loading any longer }) + test('should reset to default state when created from hydration', async () => { + const client = createQueryClient() + await client.prefetchQuery({ + queryKey: ['string'], + queryFn: () => Promise.resolve('string'), + }) + + const dehydrated = dehydrate(client) + + const hydrationClient = createQueryClient() + hydrate(hydrationClient, dehydrated) + + expect(hydrationClient.getQueryData(['string'])).toBe('string') + + const query = hydrationClient.getQueryCache().find({ queryKey: ['string'] }) + query?.reset() + + expect(hydrationClient.getQueryData(['string'])).toBe(undefined) + }) + test('should be able to refetch a cancelled query', async () => { const key = queryKey() diff --git a/packages/query-core/src/query.ts b/packages/query-core/src/query.ts index 1f6a836a98..09e43e415a 100644 --- a/packages/query-core/src/query.ts +++ b/packages/query-core/src/query.ts @@ -182,8 +182,8 @@ export class Query< this.#cache = config.cache this.queryKey = config.queryKey this.queryHash = config.queryHash - this.#initialState = config.state || getDefaultState(this.options) - this.state = this.#initialState + this.#initialState = getDefaultState(this.options) + this.state = config.state ?? this.#initialState this.scheduleGc() } get meta(): QueryMeta | undefined {