hString() creates a text virtual node from a string, making common text content easier to place inside a virtual DOM tree. section 3.5.2 section 3.6
Its small implementation has two responsibilities:
- Represent the node with type set to DOM_TYPES.TEXT. section 3.6
- Store the supplied string in the value property. section 3.6
- Let mapTextNodes() convert string children automatically, so callers can write plain strings instead of calling hString() explicitly. section 3.5.2
Direct and automatic text nodes
Both forms describe the same text children: h('div', {}, [hString('Hello '), hString('world!')]) and h('div', {}, ['Hello ', 'world!']). The second form relies on mapTextNodes(). section 3.5.2
export function hString(str) {
return { type: DOM_TYPES.TEXT, value: str }
}
Use the implementation to see the complete text-node conversion: the function returns an object containing the virtual-node type and string value. section 3.6
function mapTextNodes(children) {
return children.map((child) =>
typeof child === 'string' ? hString(child) : child
)
}
Inspect the companion mapping function that preserves existing virtual nodes while converting string children through hString(). section 3.5.2
