Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda