Skip to content

fix(query): updating with setQueryData should not affect fetchStatus #3613

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

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
15 changes: 7 additions & 8 deletions src/core/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ interface SuccessAction<TData> {
data: TData | undefined
type: 'success'
dataUpdatedAt?: number
notifySuccess?: boolean
manual?: boolean
}

interface ErrorAction<TError> {
Expand Down Expand Up @@ -195,10 +195,7 @@ export class Query<
}
}

setData(
data: TData,
options?: SetDataOptions & { notifySuccess: boolean }
): TData {
setData(data: TData, options?: SetDataOptions & { manual: boolean }): TData {
const prevData = this.state.data

// Use prev data if an isDataEqual function is defined and returns `true`
Expand All @@ -214,7 +211,7 @@ export class Query<
data,
type: 'success',
dataUpdatedAt: options?.updatedAt,
notifySuccess: options?.notifySuccess,
manual: options?.manual,
})

return data
Expand Down Expand Up @@ -538,10 +535,12 @@ export class Query<
dataUpdateCount: state.dataUpdateCount + 1,
dataUpdatedAt: action.dataUpdatedAt ?? Date.now(),
error: null,
fetchFailureCount: 0,
isInvalidated: false,
fetchStatus: 'idle',
status: 'success',
...(!action.manual && {
fetchStatus: 'idle',
fetchFailureCount: 0,
}),
}
case 'error':
const error = action.error as unknown
Expand Down
2 changes: 1 addition & 1 deletion src/core/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class QueryClient {
const defaultedOptions = this.defaultQueryOptions(parsedOptions)
return this.queryCache
.build(this, defaultedOptions)
.setData(data, { ...options, notifySuccess: false })
.setData(data, { ...options, manual: true })
}

setQueriesData<TData>(
Expand Down
2 changes: 1 addition & 1 deletion src/core/queryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ export class QueryObserver<
const notifyOptions: NotifyOptions = {}

if (action.type === 'success') {
notifyOptions.onSuccess = action.notifySuccess ?? true
notifyOptions.onSuccess = !action.manual
} else if (action.type === 'error' && !isCancelledError(action.error)) {
notifyOptions.onError = true
}
Expand Down
23 changes: 23 additions & 0 deletions src/core/tests/queryClient.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,29 @@ describe('queryClient', () => {

expect(queryCache.find(key)!.state.data).toBe(newData)
})

test('should not set isFetching to false', async () => {
const key = queryKey()
queryClient.prefetchQuery(key, async () => {
await sleep(10)
return 23
})
expect(queryClient.getQueryState(key)).toMatchObject({
data: undefined,
fetchStatus: 'fetching',
})
queryClient.setQueryData(key, 42)
expect(queryClient.getQueryState(key)).toMatchObject({
data: 42,
fetchStatus: 'fetching',
})
await waitFor(() =>
expect(queryClient.getQueryState(key)).toMatchObject({
data: 23,
fetchStatus: 'idle',
})
)
})
})

describe('setQueriesData', () => {
Expand Down