The VirtualDOM class provides a lightweight implementation of a Virtual DOM system, which is used to efficiently update the actual DOM by minimizing direct manipulations. This implementation includes key features like element creation, diffing, and efficient DOM updates.
constructor(eventManager)- Initializes a new VirtualDOM instance
- Parameters:
eventManager: Handles event management for the virtual DOM
- Properties:
tree: Stores the current virtual DOM treeevents: Reference to the event manager
Creates a virtual DOM element
- Parameters:
tag: String representing HTML tag nameattrs: Object containing element attributeschildren: Array of child elements or single child
- Returns: Virtual DOM node object
Renders the virtual DOM to the actual DOM
- Parameters:
newTree: New virtual DOM tree to rendercontainer: DOM element to render into
- Handles initial render and updates through diffing
Creates actual DOM elements from virtual nodes
- Parameters:
node: Virtual DOM node
- Returns: Real DOM element
- Handles:
- Text nodes
- Element attributes
- Event listeners
- Styles
- Child elements
Compares old and new virtual nodes to update DOM
- Parameters:
oldNode: Previous virtual nodenewNode: New virtual nodeparent: Parent DOM elementindex: Child index in parent
- Performs:
- Node addition
- Node removal
- Node replacement
- Children updates
Updates child elements efficiently using key matching
- Parameters:
parentDom: Parent DOM elementoldChildren: Previous child nodesnewChildren: New child nodes
- Features:
- Key-based matching
- Position tracking
- Efficient reordering
Determines if a node needs updating
- Parameters:
oldNode: Previous virtual nodenewNode: New virtual node
- Returns: Boolean indicating if update is needed
- Checks:
- Node types
- Text content
- Tag names
- Attributes
import {createElement,render} from 'cdn'; // you can get it from the readme file
const app = createElement('div',
{ class: 'container' },
[
createElement('h1', {}, ['Hello']),
createElement('p', {class:"paragraphe",onclick:()=>{console.log("hello world")}}, 'Virtual DOM')
]
);
let container = document.getElementById('app')
render(app);- Efficient DOM updates through diffing
- Key-based child reconciliation
- Event handling support
- Style object support
- Nested component rendering