Working of event systems in DOM and React
Tags: react, dom
Published: 2021-01-02 15:22:52

---
title: Working of event systems in DOM and React  
published: true
description: How event system works in DOM and how it is handled in React 17.
tags: #reactjs #DOM 
//cover_image: https://direct_url_to_image.jpg
---

##How the event is handled in DOM?

Suppose that you want to attach an onClick event on a button.
This can be done as follows.

```js 
 document.getElementById('myButton').onclick = function(){
    alert('Hello!');
}
```
Above is the most common way of attaching events in your DOM element. It has one drawback that only one event handler can be attached to the DOM element.

To overcome this there is a workaround. You can do something like this

```js
  let el = document.getElementById('myButton')
``` 
 and then attach events

```js
el.addEventListener( 'click', function(){
     alert('Hello!');
}, false)
```
#### Okay, but how does the event move in DOM?

To answer this suppose we have an HTML code something like this

```html
<HTML>

<head></head>

<body>
    <div id='myDiv'>
        <button id='myButton'>My button</button>
    </div>
</body>

</HTML>
``` 

When you click on the button element event "drills down" the document through its parent and then makes it way back up.
The initial "drilling down " is called the "Capture Phase" and when it circles around and goes back up to document element it is known as "bubbling phase"
 
| ![](https://dev-to-uploads.s3.amazonaws.com/i/3ixkji10ns4c805n0gd0.png) |
|:--:|
| *Take from Alison Quaglia's [Blog](https://medium.com/javascript-in-plain-english/the-3-phases-of-event-propagation-explained-f76348b5343f)* |

#### Event Delegation

Let's say that you have **<ul>** element and with several child elements **<li>** and let's say that you want to attach an onClick event on every **li** element. 


```html 

<ul id='parent'>
    <li id='item-1'>item 1</li>
    <li id='item-2'>item 2</li>
    <li id='item-3'>item 3</li>
    <li id='item-4'>item 4</li>
</ul>

```

But the problem comes when you have to add and remove **li** elements. Adding and removing event listeners to li element can be difficult. The best way you can do this is by adding an event listener to the parent element (i.e *ul*). You can do so by:-

```js
document.getElementById("parent").addEventListener("click", (e) => {
  if (e.target && e.target.nodeName === "LI") {
    // do whatever you want to do with it
  }
});
``` 
In the above code, we have attached an onClick event listener to the parent element and then we check if the targeted node is LI or not, if it is not it will be ignored else perform the operation that you want to perform.

#### What this has to do with React 17?

In React 17 the biggest change was in Event delegation. In React 16 all the events were attached at the document level. This has been changed in react 17 all event is attached at root container at which React tree is been rendered

| ![](https://dev-to-uploads.s3.amazonaws.com/i/5zlqbso7bjgisxqlekj0.png) |
|:--:|
| *Taken from [React.org](https://reactjs.org/blog/2020/10/20/react-v17.html)* |



#### Conclusion
Although, these changes won't be noticed by most of the production apps.

In this blog, I wanted to learn more in-depth about how the browser event system works and what is the major change in React 17

More in-depth documentation is there on [React.org](https://reactjs.org/blog/2020/10/20/react-v17.html).