Yogesh Chauhan's Blog

How different is Handling Events in React vs HTML DOM?

in React on May 2, 2021

If you want to add onclick event listeners to any HTML element, you can just do this:



<button onclick="doSomething()">
  Activate Lasers
</button>


In React, it’s very similar. Just like this:



<button onClick={doSomething}>
  Do Something
</button>


Two clearly visible differences in the syntax:

  • The events name is in camelCase and not in lowercase.
  • Function is passed as the event handler and not a string.

Preventing default behavior in React

In React, we can not return false to prevent the default behavior. We must use preventDefault.

HTML prevent default



<a href="#" onclick="console.log('The link was clicked.'); return false">
  Click me
</a>



React prevent default

In React, we need to add the function separately.



function ActionLink() {
  function handleClick(e) {
    e.preventDefault();
    console.log('The link was clicked.');
  }

  return (
    <a href="#" onClick={handleClick}>
      Click me
    </a>
  );
}


addEventListener in React

Actually, in React, we don’t need to call addEventListener after the DOM element is created. Instead, we provide the listener when the element is rendered initially.


this in React JSX

In a JavaScript class, by default methods are not bound. We need to bind them. So, in React, if you want to call a method without (), you need to bind the method.

For e.g.


//in this case, we need to bind the method and then pass it
onClick={this.handleClick}

Like this:


...
...

constructor(props) {
    ...

    // This binding is necessary to make `this` work in the callback
    this.handleClick = this.handleClick.bind(this);
  }

...
...

Here’s the working example:


Passing Arguments to Event Handlers

If we use an arrow function, we have to explicitly specify the event. Like this:



<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>


But, if we use bind then we the event is automatically forwarded.



<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>


Source: React Docs


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Understand Inheritance in PHP in this Basic Example (For Beginners)PHPEffects in ReactReactThe Lending Club Analysis using Logistic Regression and Random Forest in RStudioMiscellaneousALTER DATABASE in PostgreSQLPostgresHow to create a Child Theme in WordPress?WordPressHow to Display Related Posts in WordPress?WordPress