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

110
Views
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 answers
Answer question

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 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!