🌲 virtual DOM

A Virtual DOM is a paradigm for building interactive user interfaces in the browser. At its most basic level, it involves maintaining a representation of the DOM structure built with JS objects. This representation is changed and updated in response to side effects of various kinds (user initiated, data sources being updated, etc) and the DOM is then efficiently updated to reflect those changes.

Typically virtual DOM implementations have a few main pieces / bits which aim to solve the same underlying problems, below there are brief introductions to these big concepts and links to more detailed notes about each of them.

Key terminology

h / createElement / etc

Main article: πŸ‘©β€πŸ”¬ JSX

Any virtual DOM implementation needs a way to generate virtual DOM nodes. This could be done through plain JS function calls, but typically it’s done through πŸ‘©β€πŸ”¬ JSX, which is syntactic sugar over function calls to a virtual DOM object factory function of some type.

Reconciliation

Main article: πŸ•Š VDom Reconciliation

The old and new virtual DOM trees are diffed and any updates are written to the DOM. There are quite a lot of gotchas here, from how to tell when old DOM elements should be preserved to how to minimize re-renders and provide developers with access to underlying DOM nodes.

Re-Rendering

When to do it, how often, what triggers it, etc

resources for learning

I build a little implementation of the above which actually gets a little more complicated (I added state management): https://github.com/alicewriteswrongs/little-vdom

πŸ’― hundred VDom

simple-virtual-dom

See πŸ‘Ά simple-virtual-dom

This is, I think, a slightly more complicated implementation. I think it’s kind of ugly.

million

https://millionjs.org/ or https://github.com/aidenybai/million

a good bit more complicated, and it looks like it’s somewhat written to support the usecase of providing a react-compatible DX