Infinite render loop when select maps keepPreviousData to undefined in v5.105.2+
Maintainers usually reply within 1 day
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
- Domain
- backend-api-design
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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from TanStack/query
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
TanStack/query#12466 · 1 comment ·
Maintainers usually reply within 1 day
-
solid-query: STRICT_READ_UNTRACKED on Solid 2 — client()/options() read in component body (useMutation, useBaseQuery)May be free again @LeonxLJX claimed this 74 days ago, and no pull request is open. Open
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
TanStack/query#11358 · 2 comments · 1 reaction ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Maintainers usually reply within 1 day
-
infiniteQuery throws "getNextPageParam is not a function" when 2+ stale pages are cachedPossibly taken @abu-tahir-0 claimed this 7 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 42/100
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
Maintainers usually reply within 1 day
Similar issues
-
Type/Improvement
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
OpenNSW/nsw-srilanka#1259 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
oxc-project/tsrs#1010 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Fission-AI/OpenSpec#2260 ·
Maintainers usually reply within 1 day
-
Add: ESP 1 UKOpencheck:passed streams:add
Difficulty 1/5 1-3 hours Newbie friendliness 64/100
Maintainers usually reply within 1 day
-
area:infra area:storage bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
THU-MAIC/OpenMAIC#2311 · 1 comment ·
Maintainers usually reply within 1 day