Skip to content
Source
Sign inSign up
TanStack/query

Render loop since v5.105.2 when placeholderData, or its select result, is not referentially stable #12639

Closedjakubdrozdek(GitHub profile, opens in a new tab) opened this issue 0 comments

Describe the bug

Since v5.105.2, a query that shows placeholderData can re-render in a loop until React throws "Maximum update depth exceeded". This happens when the placeholder result is a new reference each time the observer computes it, and the component renders again after mount (for example, because its parent re-renders). In v5.105.1, every case below settles.

Case (the query never resolves)useQueryuseQueries
placeholderData is a class instance created on every rendersettlesloops
placeholderData is plain data whose content changes on every rendersettlesloops
placeholderData is equal plain data created on every render, with a stable select that returns a Setloopsloops
placeholderData is a stable object, with a stable select that throws for itloopsloops

This looks like the same missing fixed point in QueryObserver.createResult as #12574, reached through placeholderData instead of keepPreviousData. The last row is not #12532: the select function is stable, and useQuery loops too.

Your minimal, reproducible example

https://stackblitz.com/github/jakubdrozdek/tanstack-query-placeholder-repro?file=src%2Fissue1.jsx

Steps to reproduce

  1. Open the example. Under "Issue 1", pick a case marked "loops".
  2. Click "re-render" once. The case shows "Maximum update depth exceeded".
  3. Set @tanstack/react-query to 5.105.1 in package.json. Every case settles.

The first case, for example:

class Row {
  constructor(name) {
    this.name = name
  }
}

function Example() {
  const [result] = useQueries({
    queries: [{ queryKey: ['row'], queryFn: () => new Promise(() => {}), placeholderData: new Row('row') }],
  })
  return <p>{String(result.isPlaceholderData)}</p>
}

Expected behavior

The result settles, as in v5.105.1. A placeholder result that has not changed in meaning should not notify again only because its reference changed.

How often does this bug happen?

Every time

Platform

Not platform specific. Reproduced with React 19.2.8 in tests (Bun with happy-dom) and in the Vite example above.

Tanstack Query adapter

react-query

TanStack Query version

v5.108.1 (also v5.105.2 and v5.107.1; v5.105.1 is fine)

Additional context

On every render, useQuery and useQueries compute an optimistic result and then set the options again (#12230). replaceData cannot keep the previous reference for a class instance, a Set, or changed content, so each computation returns a new result and the observer notifies again. A select that throws creates a new error each time, with the same effect (queryObserver.ts:710 in v5.108.1).

  1. tanstack-source added labelsbug

  2. TkDodo linked this to #12649

  3. TkDodo closed this

Sign in to join this conversation.