Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What’s the difference between React event and DOM event? [closed]

I’ve tried searching for an answer to this, but couldn’t find solution on the internet.

This looks pretty interesting question. Can anyone please explain if there is a real difference.

like image 472
Hemadri Dasari Avatar asked Feb 20 '18 06:02

Hemadri Dasari


People also ask

What's the difference between synthetic React Events and JavaScript events?

HTML defines a set of events, and JavaScript uses event handlers to manage these events. React also implements event handlers, such as onClick , onMouseMove , onLoad , onError , etc. React's event handlers are named with camelCase APIs, while JavaScript event handlers are named with lowercase APIs.

Is there any difference between browser's and React's Onchange event?

The difference is that the oninput event occurs immediately after the value of an element has changed, while onchange occurs when the element loses focus, after the content has been changed. The other difference is that the onchange event also works on <select> elements.

Which of the following are differences between synthetic events and native events in React?

React synthetic events are very similar to native events, however, with synthetic events, the same API interface is implemented across multiple browsers. Both synthetic events and native events can implement the preventDefault and stopPropagation methods.


2 Answers

A React event is also known as a SyntheticEvent.

From React#SyntheticEvent:

a cross-browser wrapper around the browser’s native event. It has the same interface as the browser’s native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers

SyntheticEvent contains nativeEvent, which can be used to access browser-specific events and event-handling mechanisms.

DOM event :

nothing but a nativeEvent mapped to the browser in which the app is running.

e.g.

  1. onchange (React having onChange)

  2. onclick (React having onClick) etc.

EDIT :

e.g. using onclick < IE9

element.attachEvent('onclick', function() { /* do stuff here*/ });

other browsers (including IE 9 and above):

element.addEventListener('click', function() { /* do stuff here*/ }, false);

As we can see, we need a script to handle cross-browser compatibility. One very popular library, jQuery, has done so many such things to overcome cross-browser compatibility issues.

If you check even the jQuery docs or JavaScript (e.g. official docs) for the compatibility of different APIs for different browsers, you will easily get what exactly is meant by events work identically across all browsers.

I have taken the above snippets from here.

like image 140
RIYAJ KHAN Avatar answered Nov 15 '22 04:11

RIYAJ KHAN


React gives you a SyntheticEvent, a cross-browser wrapper around the browser’s native event. It has the same interface as the browser’s native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers.

The native event on DOM element can be accessed through nativeEvent attribute from the synthetic event.

Synthetic event contains the following attributes

boolean bubbles
boolean cancelable
DOMEventTarget currentTarget
boolean defaultPrevented
number eventPhase
boolean isTrusted
DOMEvent nativeEvent
void preventDefault()
boolean isDefaultPrevented()
void stopPropagation()
boolean isPropagationStopped()
DOMEventTarget target
number timeStamp
string type

Check the React documentation on Events

Consider the example,

DOMEvent      ReactEvent(Synthetic event)
onclick       onClick
onchange      onChange
like image 26
Shubham Khatri Avatar answered Nov 15 '22 03:11

Shubham Khatri