A DOM element is a browser node representing an HTML element, such as a paragraph, input, or button. It has a tag name and can have attributes, children, styles, and event listeners. Unlike a text node, it can contain other nodes; unlike a fragment, it is itself an element in the document tree. section 3.4 section 8.2.5
It helps to separate the browser element from its virtual description. A virtual element node is a JavaScript object with a type of 'element', a tag, props, and ordered children. The h(tag, props, children) function builds that description; it does not create the browser element. Props and children default to an empty object and array. section 3.5 section 3.2
To turn the description into a browser element:
- Call document.createElement(tag), then apply the props and retain the resulting element on the virtual node as el. section 8.1.3
- Mount each child inside the new element, then insert the element into its parent. section 8.1.3
The mounting function shows where a virtual description becomes a browser element and how its children are attached. section 8.1.3
When a view changes, reconciliation can reuse an existing element instead of creating another one. It carries the old browser element into the new virtual node, then patches attributes, CSS classes, styles, and event handlers separately. Child nodes require their own patching step. If the nodes are not considered equal, the algorithm destroys the old DOM and mounts the new description. section 8.2.5
Moving a list item
Suppose two <li> elements change positions. A key on each virtual element can help reconciliation recognize the move: equality compares both tag and key. The key guides the framework; it is removed from the props before attributes are applied to the browser element. section 12.2
