DEV Community

Discussion on: Building a Simple Virtual DOM from Scratch

Collapse
 
mart_e profile image
Martin

Hi,

Thanks a lot for the article, very nicely explained.
How would you recommend to handle events on the virtual nodes (e.g. add a addEventListener on a node that will influence another node rendering)?

Collapse
 
ycmjason profile image
YCM Jason

since many people asked. i might write an eps 2 that could cover this.

Collapse
 
csmrdb profile image
Casimir de Bruijn • Edited

Please do, if you haven't already. I enjoyed your article btw, pinned on reading list.

Currently, I have the following somewhat working:

render.ts
if (events) {
events.map(([type, event]) => {
$element.addEventListener(type, event)
})
}

index.ts
attrs: {...},
events: [['click',() => {console.log('event handled')}]]

It seems to work and not to propagate upwards. Not sure about dynamically rendering nested elements or w/e. Thoughts?