@@ -49,6 +49,12 @@ export class QueryObserver<
49
49
private currentQuery ! : Query < TQueryFnData , TError , TQueryData >
50
50
private currentResult ! : QueryObserverResult < TData , TError >
51
51
private currentResultState ?: QueryState < TQueryData , TError >
52
+ private previousOptions ?: QueryObserverOptions <
53
+ TQueryFnData ,
54
+ TError ,
55
+ TData ,
56
+ TQueryData
57
+ >
52
58
private previousQueryResult ?: QueryObserverResult < TData , TError >
53
59
private initialDataUpdateCount : number
54
60
private initialErrorUpdateCount : number
@@ -155,8 +161,7 @@ export class QueryObserver<
155
161
setOptions (
156
162
options ?: QueryObserverOptions < TQueryFnData , TError , TData , TQueryData >
157
163
) : void {
158
- const prevOptions = this . options
159
-
164
+ this . previousOptions = this . options
160
165
this . options = this . client . defaultQueryObserverOptions ( options )
161
166
162
167
if (
@@ -168,39 +173,49 @@ export class QueryObserver<
168
173
169
174
// Keep previous query key if the user does not supply one
170
175
if ( ! this . options . queryKey ) {
171
- this . options . queryKey = prevOptions . queryKey
176
+ this . options . queryKey = this . previousOptions . queryKey
172
177
}
173
178
174
179
const didUpdateQuery = this . updateQuery ( )
175
180
176
181
let optionalFetch
182
+ let updateResult
177
183
let updateStaleTimeout
178
184
let updateRefetchInterval
179
185
180
- // If we subscribed to a new query, optionally fetch and update intervals
186
+ // If we subscribed to a new query, optionally fetch and update result and timers
181
187
if ( didUpdateQuery ) {
182
188
optionalFetch = true
189
+ updateResult = true
183
190
updateStaleTimeout = true
184
191
updateRefetchInterval = true
185
192
}
186
193
187
194
// Optionally fetch if the query became enabled
188
- if ( this . options . enabled !== false && prevOptions . enabled === false ) {
195
+ if (
196
+ this . options . enabled !== false &&
197
+ this . previousOptions . enabled === false
198
+ ) {
189
199
optionalFetch = true
190
200
}
191
201
202
+ // Update result if the select function changed
203
+ if ( this . options . select !== this . previousOptions . select ) {
204
+ updateResult = true
205
+ }
206
+
192
207
// Update stale interval if needed
193
208
if (
194
- this . options . enabled !== prevOptions . enabled ||
195
- this . options . staleTime !== prevOptions . staleTime
209
+ this . options . enabled !== this . previousOptions . enabled ||
210
+ this . options . staleTime !== this . previousOptions . staleTime
196
211
) {
197
212
updateStaleTimeout = true
198
213
}
199
214
200
215
// Update refetch interval if needed
201
216
if (
202
- this . options . enabled !== prevOptions . enabled ||
203
- this . options . refetchInterval !== prevOptions . refetchInterval
217
+ this . options . enabled !== this . previousOptions . enabled ||
218
+ this . options . refetchInterval !== this . previousOptions . refetchInterval
204
219
) {
205
220
updateRefetchInterval = true
206
221
}
@@ -212,8 +227,7 @@ export class QueryObserver<
212
227
}
213
228
}
214
229
215
- // Update result when subscribing to a new query
216
- if ( didUpdateQuery ) {
230
+ if ( updateResult ) {
217
231
this . updateResult ( )
218
232
}
219
233
@@ -399,8 +413,12 @@ export class QueryObserver<
399
413
}
400
414
// Select data if needed
401
415
else if ( this . options . select && typeof state . data !== 'undefined' ) {
402
- // Use the previous select result if the query data did not change
403
- if ( this . currentResult && state . data === this . currentResultState ?. data ) {
416
+ // Use the previous select result if the query data and select function did not change
417
+ if (
418
+ this . currentResult &&
419
+ state . data === this . currentResultState ?. data &&
420
+ this . options . select === this . previousOptions ?. select
421
+ ) {
404
422
data = this . currentResult . data
405
423
} else {
406
424
data = this . options . select ( state . data )
0 commit comments