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

364
Vistas
SWR: How do you use the compare option in the SWR hook?

According to the documentation of SWR: React Hooks for Data Fetching, there is an options object which can be passed in for more control of the hook.

The documentation mentions a compare option:

compare(a, b): comparison function used to detect when returned data has changed, to avoid spurious rerenders. By default, dequal is used.

Is it something like this?

export function useUser(shouldAPIBeCalled = false) {
  const { data: user, error, mutate } = useSWR(() => shouldAPIBeCalled ? '/api/user' : null, fetcher, {

  compare: (_old, _new) => {
    return dequal(_old, _new);
  }
})

Essentially I don't want the fetcher to be called if we haven't changed the original object in my case:

If the user is still...

user: null // don't make that call again

Don't make another call because it hasn't changed!

What’s happening to me is the hook keeps firing.

I only have the hook in a Layout component to show hide navigation elements if we have a user or not,

In the submit function of the login to get the mutate function to update the user,

And in the Auth component (which wraps certain pages) to check if a user is authorized.

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
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