Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!