select that throws for placeholderData brings back the selection of data removed by resetQueries #12640
Describe the bug#
A query has data and a stable select. After resetQueries, the cache is empty and the observer falls back to placeholderData. If select throws for the placeholder, the result is status: 'error' with data set to the selection of the data that was reset, and isPlaceholderData: false. Nothing in the result shows that this value is no longer in the cache.
This happens in v5.105.1. Since v5.105.2, a React component with this setup also loops (#12639), so the example uses a plain QueryObserver.
Without placeholderData, the same reset clears the stored select error, and the old selection does not come back.
Your minimal, reproducible example#
https://stackblitz.com/github/jakubdrozdek/tanstack-query-placeholder-repro?file=src%2Fissue2.js
Steps to reproduce#
- Open the example and click "run" under "Issue 2".
- Before the reset, the result is
{ status: 'success', data: 'first', isPlaceholderData: false }. - After the reset, the result is
{ status: 'error', data: 'first', isPlaceholderData: false }, andgetQueryData(['label'])isundefined.
const client = new QueryClient()
client.setQueryData(['label'], { label: 'first' })
const observer = new QueryObserver(client, {
queryKey: ['label'],
queryFn: () => new Promise(() => {}),
enabled: false,
placeholderData: { label: null },
select: (row) => {
if (row.label === null) throw new Error('label not ready')
return row.label
},
})
observer.subscribe(() => {})
await client.resetQueries({ queryKey: ['label'] })
observer.getCurrentResult() // { status: 'error', data: 'first', isPlaceholderData: false, ... }
Expected behavior#
After the reset, the result does not include a selection of data the cache no longer holds. For example, status: 'error' with data: undefined, which is what you get without placeholderData.
How often does this bug happen?#
Every time
Platform#
Not platform specific. Reproduced with a plain QueryObserver under Bun and in the Vite example above.
Tanstack Query adapter#
vanilla
TanStack Query version#
v5.108.1 (also v5.105.1, v5.105.2 and v5.107.1)
Additional context#
When select throws, createResult sets data to this.#selectResult, the last successful selection (queryObserver.ts:710–:712 in v5.108.1). That selection can come from data that is gone. The clean-up just before it (:705) runs only when there is no data at all, and placeholderData counts as data at that point. For example, after a sign-out that resets queries, this can show the previous user's value. Related: #12564.
sakaintp(GitHub profile, opens in a new tab) linked this to #12641
tanstack-source added labelsbug
TkDodo closed this
Sign in to join this conversation.
Will be fixed in v6