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

163
Views
how to cache useRequest response?

I am using useRequest from ahooks library. I specified cacheKey in options but the same request is repeated over and over when it is supposed to be retrieved from cache.

let someVar = 'someValue'
const { data, loading } = useRequest(
    async () => someRequestThatDependOnVariable(someVar),
    {
      refreshDeps: [someVar],
      cacheKey: `request-${someVar}`,
    },
  );

someVar is updated via select field

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

but the same request is repeated over and over when it is supposed to be retrieved from cache.

This is as designed.

According to the documentation:

If options.cacheKey is set, useRequest will cache the successful data . The next time the component is initialized, if there is cached data, we will return the cached data first, and then send a new request in background

So the point of cacheKey here is to hide the loading of data by showing previously cached data while the latest data is being fetched. This does not prevent the data from being loaded.

However, if you also use the staleTime property, you can tell it not to make a new query for some number of seconds from the last successful request.

You can set the data retention time through options.staleTime. During this time, we consider the data to be fresh and will not re-initiate the request.

const { data, loading } = useRequest(
  async () => someRequestThatDependOnVariable(someVar),
  {
    refreshDeps: [someVar],
    cacheKey: `request-${someVar}`,
    staleTime: 10 * 60 * 1000 // 10 minutes in milliseconds
  },
);
about 4 years ago · Santiago Gelvez 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!