Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
Pase el elemento en sí como un parámetro onClick en lugar de una instancia de una clase

Necesito cambiar el texto interno de un botón que representa una clase. Por eso, cuando uso la palabra clave this , React imagina que quiero pasar el objeto de esa clase en lugar del elemento en sí.

Aquí tengo mi ejemplo de código React que no funciona:

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

Y aquí tengo un código JavaScript vanila que obtiene el resultado esperado

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

0

La palabra clave this en un componente React se refiere al componente en sí mismo como lo adivinó. Debe agregar un evento de parámetro al método onClick:

 <button onClick={(e) => this.helloWorld(e.target)}>Change it!</button>
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!