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

112
Vistas
Pass the element itself as an onClick parameter instead of an instance of a class

I need to change the inner text of a button that is rendered by a class. Because of that, when I use the this keyword, React imagine that I want to pass the object of that class instead of the element itself.

Here I have my sample React code that doesn't work:

class Component extends React.Component {
    
    helloWorld(element) {
        element.innerText = 'Hello Wolrd!';
    }
    render() {
        return (
            <button onClick={this.helloWorld(this)}>Change it!</button> 
        )
    }
}

And here I have a vanila JavaScript code that gets the expected result

<button onclick=helloWorld(this)>Change it!</button> 
<script>
  function helloWorld(element) {
    element.innerText='Hello World!';
  }
</script>
about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

The keyword this in a React component refers to the component itself as you guessed. You need to add a parameter event to the onClick method :

<button onClick={(e) => this.helloWorld(e.target)}>Change it!</button>
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