React Events

Table of contents
Reading Time: 2 minutes

In this blog, we will be going to get an overview of React Events, these events occur on various actions like user action or system-generated events. For eg: window resize, web page loading, keypress, mouse hover, mouse click, and many other interactive actions are known as events.

React consists of its own events handling systems which are very similar in the way we use to handling events on DOM elements. And event handling in react known as Synthetic events. The synthetic event is a cross-browser wrapper of the browser’s native event.

Note:

1: React events are named using camelCase, rather than lowercase.
2: With JSX you pass a function as the event handler, rather than a string.

Let’s see now how we declare events in plain HTML and React as well.

Event Declaration in plain HTML

<button onclick="displayText()">
Event declaration in plain HTML
</button>

Event Declaration in Reactjs

<button onClick={displayText()}
Event declaration in ReactJS
</button>

Another difference in the react event is that we cannot return false to prevent the default behavior. We must call the preventDefault event explicitly to prevent the default behavior.

In plain HTML, to prevent the default link behavior of showing alert in a text.

<a onclick="console.log('Learning react events.');
return false"> Click Me</a>

Similarly, we write the same code in react as:

function ReactActionLink() {
   function handleClick(e) { 
    e.preventDefault();
    console.log('Learning react events.');
}
return(
    <a href="#" onClick={handleClick}>Click me</a>
    );
}

In this snippet, e is a synthetic event. Also, you don’t have to worry about cross-browser compatibility. as these synthetic events define according to the W3C standards.

In the below example, we have used only one component and adding an onChange event. This event will trigger the changeMyName function, which returns the name.

import React, {Component} from 'react';
class App extends Component {
    constructor(props){
       super(props);
       this.state = {
            name: ""
        };
    }

changeMyName(event) {
   this.state({
       name: event.target.value
   });
}

render() {
   return(
      <div>
          <label htmlFor="name">Enter your name: </label> 
         <input type="text" id="name" onChange=       {this.changeMyName.bind(this)}/>
        <p>You entered: { this.state.name }</p>  
  </div>
       );
    }
}
export default App;


Conclusion:
I hope after reading this blog, you will get a basic understanding of how to use react events and how it is different from plain HTML.


Written by 

Nitin Arora is a Software Consultant at Knoldus Software LLP. He has done MCA from the Banarsidas Chandiwala Institute of Information technology, Delhi(GGSIPU). He has a graduation degree in BCA from Jamia Hamdard. He has a sound knowledge of various programming languages like C, C++, Java. Also has a deep interest in frontend development like Html, CSS, Angular, Javascript, ionic, react with redux, bootstrap. He is currently working in frontend technologies like React, Html, SCSS, Bootstrap, and Typescript. He is a focused, hardworking, team-oriented member and always exploring new Technologies, His hobbies are to play cricket, volleyball, and do coding.