Hacktoberfest 2026: as issues que os mantenedores marcaram para outubro, abertas e boas para iniciantes. Ver issues do Hacktoberfest

useBaseQuery fetches the old query key when an <Activity> tree is revealed

Aberta
#12,227 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

@lazerg já está trabalhando nisso.

Desde 21/10/2026.

  • #12230 de @lazerg — aberto
  • #12283 de @vkpdeveloper — aberto

Avaliação

Dificuldade
3/5
Tempo estimado
1-2 dias
Facilidade para iniciantes
35/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Estagnada
Stack de tecnologia
react, typescript
Domínio
frontend

Direção de pesquisa

Start in packages/react-query/src/useBaseQuery.ts and compare its subscription with the later observer.setOptions effect; inspect the corresponding useQueries flow and the referenced queryObserver.ts listener check. Reproduce with the Vitest and Testing Library example in the issue, then verify revealing the hidden tree fetches only the current key. An open linked pull request (#12230) is already addressing this issue.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

Describe the bug

If a query's key changes while its component is inside <Activity mode="hidden">, revealing the tree fetches the old key once before it switches to the new one.

useBaseQuery hands the changed options to its observer in a passive effect that is declared after the store subscription (packages/react-query/src/useBaseQuery.ts, same on main):

React.useSyncExternalStore(
  React.useCallback(
    (onStoreChange) => {
      const unsubscribe = shouldSubscribe
        ? observer.subscribe(notifyManager.batchCalls(onStoreChange))
        : noop
      observer.updateResult()
      return unsubscribe
    },
    [observer, shouldSubscribe],
  ),
  () => observer.getCurrentResult(),
  () => observer.getCurrentResult(),
)

React.useEffect(() => {
  observer.setOptions(defaultedOptions)
}, [defaultedOptions, observer])

While the tree is hidden, React re-renders it, so defaultedOptions already holds the new key, but it runs no effects. On reveal, effects mount in declaration order:

  1. The subscription runs first. observer.subscribe → onSubscribe → shouldFetchOnMount reads the observer's previous query, which is stale by now, and fetches it.
  2. Only then does setOptions move the observer to the new key, which is fetched as well.

useQueries has the same order: observer.setQueries runs in a useEffect after the subscription.

This is the query side of #11171, where <Activity> re-subscription left a useMutation stuck. That one was fixed in MutationObserver.onSubscribe (#11172, #11217).

Your minimal, reproducible example

A Vitest + Testing Library case (jsdom), no external link:

// @vitest-environment jsdom
import { Activity, Suspense } from 'react'
import { QueryClient, QueryClientProvider, useSuspenseQuery } from '@tanstack/react-query'
import { act, render, screen } from '@testing-library/react'
import { expect, it } from 'vitest'

const fetched: string[] = []

function List({ filter }: { filter: string }) {
  const { data } = useSuspenseQuery({
    queryKey: ['list', filter],
    queryFn: async () => {
      fetched.push(filter)
      return filter
    },
  })
  return <p>{data}</p>
}

it('does not fetch the old key when an Activity is revealed', async () => {
  const client = new QueryClient()
  const view = (mode: 'visible' | 'hidden', filter: string) => (
    <QueryClientProvider client={client}>
      <Activity mode={mode}>
        <Suspense fallback="loading">
          <List filter={filter} />
        </Suspense>
      </Activity>
    </QueryClientProvider>
  )

  const { rerender } = render(view('visible', 'a'))
  await screen.findByText('a')

  // Change the key while hidden: the hidden tree re-renders with 'b', but no effect runs.
  rerender(view('hidden', 'b'))
  // Let both queries go stale (suspense uses a 1s minimum staleTime).
  await act(() => new Promise((resolve) => setTimeout(resolve, 1100)))

  rerender(view('visible', 'b'))
  await screen.findByText('b')
  await act(() => new Promise((resolve) => setTimeout(resolve, 50)))

  // 'a' was fetched once, on the first render. Revealing the tree should not fetch it again.
  expect(fetched.filter((key) => key === 'a')).toEqual(['a'])
})

On 5.101.2 this fails with expected [ 'a', 'a' ] to deeply equal [ 'a' ].

Steps to reproduce
  1. Render a component that calls useSuspenseQuery (or useQuery) inside <Activity mode="visible">.
  2. Switch the Activity to hidden, and change the query key in the same update.
  3. Wait past staleTime (1s under suspense).
  4. Switch the Activity back to visible.
  5. The queryFn runs for the old key, then for the new key.
Expected behavior

Revealing the tree should fetch only the current key. The old key is no longer rendered, and no observer should start a fetch for it.

In our app, the old key named a group the user had just deleted on another tab. Every return to the tab sent requests for that group's records, and they came back 404.

How often does this bug happen?

Every time

Platform
  • OS: macOS
  • Browser: Chromium (also reproduced in jsdom, above)
Tanstack Query adapter

react-query

TanStack Query version

5.101.2 (the code is unchanged on main)

TypeScript version

5.9.3

Additional context
  • React / React DOM 19.2.3, Next.js 16.3.8 (App Router).

  • A fix that works for us: sync the options in a layout effect. Layout effects run before passive effects, including when an <Activity> tree is revealed. setOptions does not fetch while the observer has no listeners (queryObserver.ts, const mounted = this.hasListeners()). So the passive subscription then starts on the current query:

    React.useLayoutEffect(() => {
      observer.setOptions(defaultedOptions)
    }, [defaultedOptions, observer])
    

    We apply the same change to observer.setQueries in useQueries. With that patch on 5.101.2, the case above passes, and so do the suites of our two React apps that use TanStack Query (about 6,000 tests).

  • Another option would be to update the observer's query inside the subscribe callback, before observer.subscribe, with the options from the latest render.

Linguagem predominante
TypeScript
Estrelas
50.4k
Forks
4.3k
Merge médio
9h 18min
PRs com merge (30d)
599

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de TanStack/query

Todas as issues de TanStack/query

Issues semelhantes

Mais issues de TypeScript

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.