Skip to content

Commit 509bad9

Browse files
committed
fix(queryObserver): defer tracking of error prop when useErrorBoundary is on
adding "error" to the list of tracked properties will result in us _only_ tracking error if we want to track all properties implicitly by _not_ observing any properties (because we check for trackedProps.size). Moving the adding of "error" to _after_ the size check fixes this
1 parent 5e2f2c2 commit 509bad9

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

src/core/queryObserver.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DefaultedQueryObserverOptions, RefetchPageFilters } from './types'
1+
import { RefetchPageFilters } from './types'
22
import {
33
isServer,
44
isValidTimeout,
@@ -224,14 +224,7 @@ export class QueryObserver<
224224
}
225225

226226
trackResult(
227-
result: QueryObserverResult<TData, TError>,
228-
defaultedOptions: DefaultedQueryObserverOptions<
229-
TQueryFnData,
230-
TError,
231-
TData,
232-
TQueryData,
233-
TQueryKey
234-
>
227+
result: QueryObserverResult<TData, TError>
235228
): QueryObserverResult<TData, TError> {
236229
const trackedResult = {} as QueryObserverResult<TData, TError>
237230

@@ -246,10 +239,6 @@ export class QueryObserver<
246239
})
247240
})
248241

249-
if (defaultedOptions.useErrorBoundary) {
250-
this.trackedProps.add('error')
251-
}
252-
253242
return trackedResult
254243
}
255244

@@ -606,6 +595,10 @@ export class QueryObserver<
606595

607596
const includedProps = new Set(notifyOnChangeProps ?? this.trackedProps)
608597

598+
if (this.options.useErrorBoundary) {
599+
includedProps.add('error')
600+
}
601+
609602
return Object.keys(result).some(key => {
610603
const typedKey = key as keyof QueryObserverResult
611604
const changed = result[typedKey] !== prevResult[typedKey]

src/reactjs/tests/useQuery.test.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,6 +2672,30 @@ describe('useQuery', () => {
26722672
consoleMock.mockRestore()
26732673
})
26742674

2675+
it('should update with data if we observe no properties and useErrorBoundary', async () => {
2676+
const key = queryKey()
2677+
2678+
let result: UseQueryResult<string> | undefined
2679+
2680+
function Page() {
2681+
const query = useQuery(key, () => Promise.resolve('data'), {
2682+
useErrorBoundary: true,
2683+
})
2684+
2685+
React.useEffect(() => {
2686+
result = query
2687+
})
2688+
2689+
return null
2690+
}
2691+
2692+
renderWithClient(queryClient, <Page />)
2693+
2694+
await sleep(10)
2695+
2696+
expect(result?.data).toBe('data')
2697+
})
2698+
26752699
it('should set status to error instead of throwing when error should not be thrown', async () => {
26762700
const key = queryKey()
26772701
const consoleMock = mockConsoleError()

src/reactjs/useBaseQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export function useBaseQuery<
134134

135135
// Handle result property usage tracking
136136
if (!defaultedOptions.notifyOnChangeProps) {
137-
result = observer.trackResult(result, defaultedOptions)
137+
result = observer.trackResult(result)
138138
}
139139

140140
return result

0 commit comments

Comments
 (0)