Skip to content

Commit cfd6133

Browse files
authored
fix(core): Infinite render loops in react-query v4.29.22 and up (#5839)
* fix: Infinite render loops in react-query * Add test case for infinite render loop
1 parent 9b8b5f0 commit cfd6133

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

packages/query-core/src/queryObserver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ function shouldAssignObserverCurrentProperties<
834834

835835
// if the newly created result isn't what the observer is holding as current,
836836
// then we'll need to update the properties as well
837-
if (observer.getCurrentResult() !== optimisticResult) {
837+
if (!shallowEqualObjects(observer.getCurrentResult(), optimisticResult)) {
838838
return true
839839
}
840840

packages/react-query/src/__tests__/useQuery.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6211,4 +6211,25 @@ describe('useQuery', () => {
62116211
await waitFor(() => rendered.getByText('Rendered Id: 2'))
62126212
expect(spy).toHaveBeenCalledTimes(1)
62136213
})
6214+
it('should not cause an infinite render loop when using unstable callback ref', async () => {
6215+
const key = queryKey()
6216+
6217+
function Test() {
6218+
const [_, setRef] = React.useState<HTMLDivElement | null>()
6219+
6220+
const { data } = useQuery({
6221+
queryKey: [key],
6222+
queryFn: async () => {
6223+
await sleep(5)
6224+
return 'Works'
6225+
},
6226+
})
6227+
6228+
return <div ref={(value) => setRef(value)}>{data}</div>
6229+
}
6230+
6231+
const rendered = renderWithClient(queryClient, <Test />)
6232+
6233+
await waitFor(() => rendered.getByText('Works'))
6234+
})
62146235
})

0 commit comments

Comments
 (0)