The h function: Describe pages with JavaScript objects
🎯 Introduction
In the previous article, we created our first virtual node with h('div#container', 'Hello Snabbdom'). Now we will explore common ways to call h, its selector syntax, and the virtual nodes it returns. You will then be able to describe any page structure using JavaScript objects.
🧱 What does h do?
The h function has one job: create virtual nodes (VNodes). It takes a description of an element and returns an ordinary JavaScript object called a virtual node.
import { h } from 'snabbdom';
const vnode = h('div', 'Some text');
console.log(vnode);
The logged object looks roughly like this:
{
sel: 'div',
data: {},
text: 'Some text',
children: undefined,
elm: undefined,
key: undefined
}
Here is what each field means:
| Field | Meaning |
|---|---|
sel | Selector describing the tag name, id, and class |
data | Data object for styles, properties, attributes, events, and more (covered next) |
text | Text content |
children | Array of child nodes |
elm | Corresponding real DOM element, populated by Snabbdom after rendering |
key | Unique node identifier (covered in article 5) |
Pay particular attention to elm: a virtual node corresponds to a real DOM node. After rendering, Snabbdom stores the real element in elm so it can find the target for subsequent updates.
🧱 Three common forms
h is flexible. These are its three most common forms:
// 1. Selector only: an empty element
h('div');
// 2. Selector + text: h(sel, text)
h('p', 'A paragraph');
// 3. Selector + array of children: h(sel, [children])
h('ul', [
h('li', 'Apple'),
h('li', 'Banana')
]);
There is also a fourth form, h(sel, data, text/children). The middle argument, data, supplies styles, events, and other data. We will cover it in the next article.
h('div', h('span', 'Only child')). Numbers are automatically converted to text: h('p', 100) renders 100.🧱 Selector syntax
The first argument to h is sel, the selector. Its syntax resembles CSS selectors, but supports only tag names, #id, and .class:
h('div'); // <div></div>
h('div#app'); // <div id="app"></div>
h('div.box'); // <div class="box"></div>
h('div#app.box.active'); // <div id="app" class="box active"></div>
h('span'); // <span></span>
A useful guideline: put IDs and classes in the selector, and other attributes such as href and placeholder in data. Do not try to fit them into the selector.
input[type=text] and descendant selectors such as div p are not supported. Use the children array to describe nested structures.💡 Nesting: describe a real page
Real pages contain nested elements. Describe each level using a children array. For example:
<div id="box">
<h1 class="title">Fruit list</h1>
<ul>
<li>Apple</li>
<li>Banana</li>
<li>Orange</li>
</ul>
</div>
The corresponding virtual DOM is:
const vnode = h('div#box', [
h('h1.title', 'Fruit list'),
h('ul', [
h('li', 'Apple'),
h('li', 'Banana'),
h('li', 'Orange')
])
]);
This renders exactly like the handwritten HTML. Does it look familiar? Compiled Vue templates also produce virtual node trees like this one.
❓ Questions and answers
Q1: Can text and children be used together?
No. A virtual node can contain text through text or an array of child nodes through children; they are mutually exclusive. Snabbdom follows this rule during updates too, as we will see in article 6 on diffing.
Q2: What happens if h('div') receives no content?
It renders an empty <div></div>. With neither text nor children, it is simply an empty element.
Q3: Can a selector contain multiple IDs?
The syntax does not produce an error, but HTML permits only one ID per element. Multiple IDs serve no useful purpose here and should not be used in practice.
Q4: Can I render the same virtual node object more than once?
This is not recommended. Once rendered, a virtual node has an elm reference to its real element. Reusing the same object can make that reference incorrect. For repeated structures, write a function that calls h to create a fresh object each time.
🧪 Exercise
Use h to describe this structure and render it on the page:
<div id="card">
<h2 class="title">Profile card</h2>
<p class="intro">A beginner in frontend development</p>
</div>
import { init, h } from 'snabbdom';
const patch = init([]);
// Write your code here.
🎉 You now know how to use h! Next, we will learn how modules add styles, properties, attributes, and events to virtual nodes.
