Infinite render loop when select maps keepPreviousData to undefined in v5.105.2+

Open
#12,574 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@mooxl is already working on this.

Since Nov 10, 2026.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
react, typescript

Research direction

Start with the linked reproduction and compare behavior in v5.105.1 with v5.107.1; the issue points to QueryObserver.createResult and the repeated getOptimisticResult and setOptions calls in useQuery. Reproduce by rendering Picker and clicking its button, then trace why the placeholder result alternates between success and pending. Done when the result settles with status: 'success', isPlaceholderData: true, and data: undefined, without a React update-depth error.

Written by the indexing model from the issue text.

Description

Describe the bug

Since v5.105.2, a disabled query with placeholderData: keepPreviousData and a select that returns undefined loops until React throws "Maximum update depth exceeded". Unlike #12532, select does not throw. Still reproducible on v5.107.1 and main; v5.105.1 is fine.

Your minimal, reproducible example

https://stackblitz.com/github/mooxl/tanstack-query-12574-repro?file=src%2Fmain.jsx

Click the button. Switch @tanstack/react-query to 5.105.1 in package.json and it settles instead.

function Picker() {
  const [id, setId] = useState('a')
  const { data } = useQuery({
    queryKey: ['items', id],
    queryFn: () => [id],
    enabled: id !== '',
    placeholderData: keepPreviousData,
    select: (items) => items.find((item) => item === id),
  })
  return <button onClick={() => setId('')}>{data ?? 'none'}</button>
}
Steps to reproduce

Render Picker, wait for a, click the button.

Expected behavior

The result settles as placeholder data (status: 'success', isPlaceholderData: true, data: undefined).

How often does this bug happen?

Every time

Platform

Any

Tanstack Query adapter

react-query

TanStack Query version

5.107.1

Additional context

QueryObserver.createResult has no fixed point here. When the previous result was a placeholder, it reuses prevResult.data, which select already mapped to undefined, so the result falls back to pending. The next call computes the placeholder again (success). #12230 makes useQuery call getOptimisticResult and then setOptions on every render, which keeps flipping it. It also reproduces with QueryObserver alone.

Dominant language
TypeScript
Stars
50.6k
Forks
4.2k
Avg merge
8h 37m
Merged PRs (30d)
450

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from TanStack/query

All issues in TanStack/query

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.