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

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

<button onClick={doSomething}>
  Do Something

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

React prevent default

In React, we need to add the function separately.

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

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

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

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 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS? #Aug 1 What is Zdog? #Aug 1 How to add before after image effect using pure CSS? #Jul 31 Add animation to your skills bar using CSS #Jul 31 Use SwiperJS to create mobile touch sliders fast
You might also like these
A few HTML coding standards from WordPressHTMLHow to remove N/A from Radio Button list in Drupal?DrupalKilling A Project Part 1: What criteria should be used to cancel/kill a project?MiscellaneousWhat is Hoisting in JavaScript?JavaScriptCONCAT and CONCAT_WS Functions in PostgresPostgresHow to remove WordPress TinyMCE Editor buttons?WordPress