From 509bad902e94841ce4c19f354aec4d26f5f8daff Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Sat, 11 Dec 2021 11:32:32 +0100 Subject: [PATCH] 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 --- src/core/queryObserver.ts | 19 ++++++------------- src/reactjs/tests/useQuery.test.tsx | 24 ++++++++++++++++++++++++ src/reactjs/useBaseQuery.ts | 2 +- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/core/queryObserver.ts b/src/core/queryObserver.ts index 526bb07593..df0dd565bf 100644 --- a/src/core/queryObserver.ts +++ b/src/core/queryObserver.ts @@ -1,4 +1,4 @@ -import { DefaultedQueryObserverOptions, RefetchPageFilters } from './types' +import { RefetchPageFilters } from './types' import { isServer, isValidTimeout, @@ -224,14 +224,7 @@ export class QueryObserver< } trackResult( - result: QueryObserverResult, - defaultedOptions: DefaultedQueryObserverOptions< - TQueryFnData, - TError, - TData, - TQueryData, - TQueryKey - > + result: QueryObserverResult ): QueryObserverResult { const trackedResult = {} as QueryObserverResult @@ -246,10 +239,6 @@ export class QueryObserver< }) }) - if (defaultedOptions.useErrorBoundary) { - this.trackedProps.add('error') - } - return trackedResult } @@ -606,6 +595,10 @@ export class QueryObserver< const includedProps = new Set(notifyOnChangeProps ?? this.trackedProps) + if (this.options.useErrorBoundary) { + includedProps.add('error') + } + return Object.keys(result).some(key => { const typedKey = key as keyof QueryObserverResult const changed = result[typedKey] !== prevResult[typedKey] diff --git a/src/reactjs/tests/useQuery.test.tsx b/src/reactjs/tests/useQuery.test.tsx index b8b04fbc13..29cc7666f3 100644 --- a/src/reactjs/tests/useQuery.test.tsx +++ b/src/reactjs/tests/useQuery.test.tsx @@ -2672,6 +2672,30 @@ describe('useQuery', () => { consoleMock.mockRestore() }) + it('should update with data if we observe no properties and useErrorBoundary', async () => { + const key = queryKey() + + let result: UseQueryResult | undefined + + function Page() { + const query = useQuery(key, () => Promise.resolve('data'), { + useErrorBoundary: true, + }) + + React.useEffect(() => { + result = query + }) + + return null + } + + renderWithClient(queryClient, ) + + await sleep(10) + + expect(result?.data).toBe('data') + }) + it('should set status to error instead of throwing when error should not be thrown', async () => { const key = queryKey() const consoleMock = mockConsoleError() diff --git a/src/reactjs/useBaseQuery.ts b/src/reactjs/useBaseQuery.ts index a5f4d5f5dd..3a99bd1938 100644 --- a/src/reactjs/useBaseQuery.ts +++ b/src/reactjs/useBaseQuery.ts @@ -134,7 +134,7 @@ export function useBaseQuery< // Handle result property usage tracking if (!defaultedOptions.notifyOnChangeProps) { - result = observer.trackResult(result, defaultedOptions) + result = observer.trackResult(result) } return result