YogeshChauhan . com

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

dreamhost

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 uninstall Cocoapods from the Mac OS? #4 How to add Read More Read Less Button using JavaScript? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

May 5 Use inline if to make a shorter conditional syntax in React May 4 What is Conditional Rendering in React? May 3 How does @extend rule work in SCSS (Sass)? May 2 How different is Handling Events in React vs HTML DOM? May 2 How to create bouncing balls using HTML canvas and JavaScript? Apr 30 HTML canvas methods

You might also like these

What is PostgreSQL? How similar or different it is from SQL?PostgresHow to create a Random Hex Color generator using JavaScript?JavaScriptHow to convert a number rounding to a specified number of decimals in JavaScript?JavaScriptHow to create a Progress Bar using JavaScript?JavaScript5 Key Principles Of Good Website UsabilityUI/UXSafe Practice to add Links to cross-origin destinationUI/UX