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.

#event #react ##events in reactjs ##react-events ##reactjs #react native

React Events
2.10 GEEK