Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
Do react query result objects go stale due to closures?

I have always felt like I have a pretty solid understanding of how JS closures work, but since using ReactJS for the last couple of months there is something I can't quite make sense of.

In the below example, will the query result object getData be stale if used in the success callback for the other query?

My closure instincts tell me yes, but my reactjs instincts tell me no for some reason and I can't make sense of why it works the way that it does.

All help is appreciated!

/*
* Custom query hooks
*/
function useGetData(someId, opts){
  return useQuery(['getData', someId], 
    getDataQueryFn,
    {
      ...opts,
    },
  );
}

function useOtherData(someId, opts){
  return useQuery(['otherData', someId], 
    otherDataQueryFn,
    {
      ...opts,
    },
  );
}

/*
* React component
*/
function MyComponent(){
  const getData = useGetData("1");
  const otherData = useOtherData("1", {
    onSuccess: () => {
      // QUESTION: if I use getData here, won't it be stale due to closure? why or why not?
    }
  });

  // render and stuff...
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It depends on useQuery implementation. However I am pretty sure it wont use stale data. I expect following order:

  • Each time result of useGetData is updated - re-render happens, due to some internal state update
  • new onSuccess callback (capturing latest getData result in closure) is passed via props to useOtherData and picked up by library to handle next update
about 4 years ago · Juan Pablo Isaza Relatório

0

It doesn't go stale. Here's a codesandbox that closures over a counter and the fetch needs 5 seconds to complete. relevant code from the sandbox:

  const [count, inc] = React.useReducer((val) => val + 1, 0);
  const { data } = useQuery(
    "repoData",
    async () => {
      await waitFor(5000);
      return Promise.resolve({ name: "foo " });
    },
    {
      onSuccess: () => console.log(count)
    }
  );

if you click the counter a couple of times in the first 5 seconds, the latest count will still be logged. It doesn't really matter if you closure over local state or over some other query.

The reason is likely that react-query will only trigger a re-render after the Promise is successful (it's a simple forced re-render via useState under the hood), and at that time, react already "sees" the latest value, not the value from the time when the query started to fetch.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda