Skip to content

Commit 9e77be1

Browse files
committed
Call cache onSuccess when data changed via setQueryData
1 parent 21fa1ec commit 9e77be1

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/core/query.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ export class Query<
470470

471471
if (typeof updatedData !== 'undefined' && !Object.is(prevData, updatedData)) {
472472
// Notify cache callback
473-
this.cache.config.onSuccess?.(data, this as Query<any, any, any, any>)
473+
this.cache.config.onSuccess?.(updatedData, this as Query<any, any, any, any>)
474474
}
475475

476476
// Remove query after fetching if cache time is 0

src/core/queryClient.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import type {
3030
ResetQueryFilters,
3131
SetDataOptions,
3232
} from './types'
33-
import type { QueryState } from './query'
33+
import type { Query, QueryState } from './query'
3434
import { QueryCache } from './queryCache'
3535
import { MutationCache } from './mutationCache'
3636
import { focusManager } from './focusManager'
@@ -136,7 +136,15 @@ export class QueryClient {
136136
let query = this.queryCache.find<TData>(queryKey)
137137

138138
if (query) {
139-
return query.setData(updater, options)
139+
const prevData = query.state.data
140+
const updatedData = query.setData(updater, options)
141+
142+
if (typeof updatedData !== 'undefined' && !Object.is(prevData, updatedData)) {
143+
// Notify cache callback
144+
this.queryCache.config.onSuccess?.(updatedData, query as Query<any, any, any, any>)
145+
}
146+
147+
return updatedData
140148
}
141149

142150
const data = functionalUpdate(updater, undefined)
@@ -148,7 +156,9 @@ export class QueryClient {
148156
const parsedOptions = parseQueryArgs(queryKey)
149157
const defaultedOptions = this.defaultQueryOptions(parsedOptions)
150158
query = this.queryCache.build(this, defaultedOptions)
151-
query.setData(data, options)
159+
const updatedData = query.setData(data, options)
160+
this.queryCache.config.onSuccess?.(updatedData, query as Query<any, any, any, any>)
161+
return updatedData
152162
}
153163

154164
setQueriesData<TData>(

0 commit comments

Comments
 (0)