Skip to content

Instantly share code, notes, and snippets.

@krcrawford
Created March 9, 2019 00:23
Show Gist options
  • Save krcrawford/c8dbe40ec53275242fa6cad13a745d65 to your computer and use it in GitHub Desktop.
Save krcrawford/c8dbe40ec53275242fa6cad13a745d65 to your computer and use it in GitHub Desktop.
VDOM - render
const render = (vNode) => {
if (typeof vNode === 'string') {
return document.createTextNode(vNode);
}
const { tagName, attrs, children } = vNode;
const el = document.createElement(tagName);
for (const [k, v] of Object.entries(attrs)) {
if (/^on/.test(k)) {
const eventType = k.replace(/^on/, '').toLowerCase();
el.addEventListener(eventType, v);
} else {
el.setAttribute(k, v);
}
}
for (const child of children) {
el.appendChild(render(child));
}
render el;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment