Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stable-selector-error-timestamp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/query-core': patch
---

Keep the timestamp stable for the same selector error to prevent infinite render loops.
20 changes: 20 additions & 0 deletions packages/query-core/src/__tests__/queryObserver.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,26 @@ describe('queryObserver', () => {
expect(observerResult2.data).toMatchObject({ myCount: 1 })
})

it('should keep the result stable when updating a cached selector error', () => {
const error = new Error('selector error')
const observer = new QueryObserver(queryClient, {
queryKey: createKey(),
initialData: 'data',
enabled: false,
select: () => {
throw error
},
})
const result = observer.getCurrentResult()

expect(result).toMatchObject({ status: 'error', error })

vi.advanceTimersByTime(1)
observer.updateResult()

expect(observer.getCurrentResult()).toBe(result)
})

it('should always run the selector again if selector throws an error and selector is not referentially stable', async () => {
const key = createKey()
const results: Array<QueryObserverResult> = []
Expand Down
3 changes: 2 additions & 1 deletion packages/query-core/src/queryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,8 @@ export class QueryObserver<
if (this.#selectError) {
error = this.#selectError
data = this.#selectResult
errorUpdatedAt = Date.now()
errorUpdatedAt =
prevResult?.error === error ? prevResult.errorUpdatedAt : Date.now()
status = 'error'
isPlaceholderData = false
}
Expand Down
Loading