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 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
Killing A Project Part 2: Who should make the decision to kill a project?MiscellaneousThe difference between Class declarations and Class expressions in JavaScriptJavaScriptSQL Inner JoinSQL/MySQLWhen you don’t want to @forward every member in SCSS (Sass)SCSSDROP DATABASE (remove a database) from PostgresPostgresWordPress: How to query all posts from custom post type and display them in a list?WordPress