Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): query should reset to default state even when created from hydration #7837

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion packages/query-core/src/__tests__/query.test.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions packages/query-core/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading