Skip to content

Fix: Infinite render loops in react-query v4.29.22 and up #5839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 5, 2023
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
2 changes: 1 addition & 1 deletion packages/query-core/src/queryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ function shouldAssignObserverCurrentProperties<

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

Expand Down
21 changes: 21 additions & 0 deletions packages/react-query/src/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6211,4 +6211,25 @@ describe('useQuery', () => {
await waitFor(() => rendered.getByText('Rendered Id: 2'))
expect(spy).toHaveBeenCalledTimes(1)
})
it('should not cause an infinite render loop when using unstable callback ref', async () => {
const key = queryKey()

function Test() {
const [_, setRef] = React.useState<HTMLDivElement | null>()

const { data } = useQuery({
queryKey: [key],
queryFn: async () => {
await sleep(5)
return 'Works'
},
})

return <div ref={(value) => setRef(value)}>{data}</div>
}

const rendered = renderWithClient(queryClient, <Test />)

await waitFor(() => rendered.getByText('Works'))
})
})