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

Ouverte
#12,574 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

@mooxl y travaille déjà.

Depuis le 10/11/2026.

  • #12575 par @mooxl — ouverte

Évaluation

Difficulté
3/5
Temps estimé
1-2 jours
Accessibilité débutants
35/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
À l'abandon
Stack technique
react, typescript

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

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.

Langage dominant
TypeScript
Étoiles
50.6k
Forks
4.2k
Merge moyen
8 h 37 min
PR mergées (30 j)
450

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de TanStack/query

Toutes les issues de TanStack/query

Issues similaires

Plus d'issues TypeScript

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.