Created
March 9, 2019 00:23
-
-
Save krcrawford/c8dbe40ec53275242fa6cad13a745d65 to your computer and use it in GitHub Desktop.
VDOM - render
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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