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

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

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 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