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

203
Vistas
arrow function in jsx
const MyComp = () => <button onClick={() => nagivate('/')}>home</button>

I have code above where I put arrow function inside my onClick handler. My colleague asked me to do this instead:

const MyComp = () => {
   const handleClick = () => nagivate('/')
   return <button onClick={handleClick}>home</button>
}

What is the point of it? he argue we can have performance issue. I'm confused.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Both versions are equivalent. The handleClick function will we redefined on every render. To improve performance use useCallback(() => nagivate('/'), [nagivate]). Then the function will only change if the navigate function changes.

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you pass anonymous function as event handler it will cause function recomputation(recreation) on each rerender, thus forcing element which exposed you event to also rerender since it "thinks" that new handler is passed because of new function reference of your anonymous event handler. If function recomputation affects your UI performance I suggest you to wrap handler in useCallback and like that you will have stable reference of you handler. At the end I disagree with you colleague that this(one simple button) will have impact on you performance, if it does it means that your upper levels are the ones that needs optimizations. It is cleaner but in terms of performance, in this specific case, almost no difference.

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