@@ -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 ,
@@ -304,20 +298,35 @@ export class QueryObserver<TResult, TError> {
304298 _state : QueryState < TResult , TError > ,
305299 action : Action < TResult , TError >
306300 ) : void {
307- this . currentResult = this . createResult ( )
301+ const { config } = this
308302
309- const { data, error, isSuccess, isError } = this . currentResult
303+ // Store current result and get new result
304+ const prevResult = this . currentResult
305+ this . currentResult = this . createResult ( )
306+ const result = this . currentResult
310307
311- if ( action . type === 'Success' && isSuccess ) {
312- this . config . onSuccess ?.( data ! )
313- this . config . onSettled ?.( data ! , null )
308+ // We need to check the action because the state could have
309+ // transitioned from success to success in case of `setQueryData`.
310+ if ( action . type === 'Success' && result . isSuccess ) {
311+ config . onSuccess ?.( result . data ! )
312+ config . onSettled ?.( result . data ! , null )
314313 this . updateTimers ( )
315- } else if ( action . type === 'Error' && isError ) {
316- this . config . onError ?.( error ! )
317- this . config . onSettled ?.( undefined , error ! )
314+ } else if ( action . type === 'Error' && result . isError ) {
315+ config . onError ?.( result . error ! )
316+ config . onSettled ?.( undefined , result . error ! )
318317 this . updateTimers ( )
319318 }
320319
321- this . updateListener ?.( this . currentResult )
320+ // Decide if we need to notify the listener
321+ const notify =
322+ // Always notify on data or error change
323+ result . data !== prevResult . data ||
324+ result . error !== prevResult . error ||
325+ // Maybe notify on other changes
326+ config . notifyOnStatusChange
327+
328+ if ( notify ) {
329+ this . updateListener ?.( result )
330+ }
322331 }
323332}
0 commit comments