@@ -217,22 +217,16 @@ export class QueryObserver<TResult, TError> {
217217 }
218218
219219 private createResult ( ) : QueryResult < TResult , TError > {
220- const { currentResult, currentQuery, previousResult, config } = this
221-
222- const {
223- canFetchMore,
224- error,
225- failureCount,
226- isFetched,
227- isFetching,
228- isFetchingMore,
229- isLoading,
230- } = currentQuery . state
231-
232- let { data, status, updatedAt } = currentQuery . state
220+ const { currentQuery, currentResult, previousResult, config } = this
221+ const { state } = currentQuery
222+ let { data, status, updatedAt } = state
233223
234224 // Keep previous data if needed
235- if ( config . keepPreviousData && isLoading && previousResult ?. isSuccess ) {
225+ if (
226+ config . keepPreviousData &&
227+ state . isLoading &&
228+ previousResult ?. isSuccess
229+ ) {
236230 data = previousResult . data
237231 updatedAt = previousResult . updatedAt
238232 status = previousResult . status
@@ -256,15 +250,15 @@ export class QueryObserver<TResult, TError> {
256250
257251 return {
258252 ...getStatusProps ( status ) ,
259- canFetchMore,
253+ canFetchMore : state . canFetchMore ,
260254 clear : this . clear ,
261255 data,
262- error,
263- failureCount,
256+ error : state . error ,
257+ failureCount : state . failureCount ,
264258 fetchMore : this . fetchMore ,
265- isFetched,
266- isFetching,
267- isFetchingMore,
259+ isFetched : state . isFetched ,
260+ isFetching : state . isFetching ,
261+ isFetchingMore : state . isFetchingMore ,
268262 isStale,
269263 query : currentQuery ,
270264 refetch : this . refetch ,
@@ -303,20 +297,35 @@ export class QueryObserver<TResult, TError> {
303297 _state : QueryState < TResult , TError > ,
304298 action : Action < TResult , TError >
305299 ) : void {
306- this . currentResult = this . createResult ( )
300+ const { config } = this
307301
308- const { data, error, isSuccess, isError } = this . currentResult
302+ // Store current result and get new result
303+ const prevResult = this . currentResult
304+ this . currentResult = this . createResult ( )
305+ const result = this . currentResult
309306
310- if ( action . type === 'Success' && isSuccess ) {
311- this . config . onSuccess ?.( data ! )
312- this . config . onSettled ?.( data ! , null )
307+ // We need to check the action because the state could have
308+ // transitioned from success to success in case of `setQueryData`.
309+ if ( action . type === 'Success' && result . isSuccess ) {
310+ config . onSuccess ?.( result . data ! )
311+ config . onSettled ?.( result . data ! , null )
313312 this . updateTimers ( )
314- } else if ( action . type === 'Error' && isError ) {
315- this . config . onError ?.( error ! )
316- this . config . onSettled ?.( undefined , error ! )
313+ } else if ( action . type === 'Error' && result . isError ) {
314+ config . onError ?.( result . error ! )
315+ config . onSettled ?.( undefined , result . error ! )
317316 this . updateTimers ( )
318317 }
319318
320- this . updateListener ?.( this . currentResult )
319+ // Decide if we need to notify the listener
320+ const notify =
321+ // Always notify on data or error change
322+ result . data !== prevResult . data ||
323+ result . error !== prevResult . error ||
324+ // Maybe notify on other changes
325+ config . notifyOnStatusChange
326+
327+ if ( notify ) {
328+ this . updateListener ?.( result )
329+ }
321330 }
322331}
0 commit comments