What is a Virtual Node?
A virtual node (VNode) is a lightweight JavaScript object that represents a piece of the UI. Instead of manipulating the DOM directly, O! works with these simple objects and efficiently updates the real DOM based on changes to the virtual representation.VNode Structure
Every virtual node in O! is an object with three properties:Property Breakdown
e(element): The node name (HTML tag like'div'or'button') or a functional component functionp(properties): An object containing the node’s attributes and propertiesc(children): An array of child VNodes or text content
The single-letter property names (
e, p, c) are used intentionally to reduce the minified JavaScript code size. This keeps O! under 1KB!Creating Virtual Nodes
Virtual nodes are created using theh() function:
With Children
Virtual nodes can contain other virtual nodes as children:With Properties
Properties map directly to DOM node properties:Text Nodes
Text content is represented as simple strings in the children array. When rendered, O! converts these to DOM text nodes:Real-World Example
Here’s how a counter button structure looks as a virtual node:How VNodes Represent UI
Virtual nodes create a tree structure that mirrors the DOM:- Lightweight: VNodes are plain JavaScript objects, much faster to create and compare than real DOM nodes
- Declarative: You describe what the UI should look like, not how to build it
- Efficient: O! can diff two VNode trees and only update the parts of the DOM that changed
Component VNodes
Virtual nodes can also represent functional components by using a function as thee property:
Next Steps
- Learn about Functional Components that return VNodes
- Explore the Template Syntax for easier VNode creation
- See the h() function API reference