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

149
Vistas
Event handler execution inside react function component

When we set an event handler for a function component, how does it access to function component local variables? because when the component was being rendered the local variables created and when it return the JSX code, the local variables removed. So how does the event handler in this code log c in the console when the c should not exist? The clickHandler function called after the component rendered.

function Sample() {
    let c = 0;
    const clickHandler = () => {
        console.log(c);
    }
    return <h2 onClick={clickHandler}>'Hello'</h2>
}
    export default Sample;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

How clickHandler is able to access a local variable (c) is what's called a closure.

Closure lets a function access the variables in its lexical scope (where is written) even when it's passed as an argument elsewhere.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures

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