Skip to content

Instantly share code, notes, and snippets.

@monochromer
Created June 18, 2017 17:31
Show Gist options
  • Save monochromer/5750e7221c1e63ca1308ec315b62c233 to your computer and use it in GitHub Desktop.
Save monochromer/5750e7221c1e63ca1308ec315b62c233 to your computer and use it in GitHub Desktop.
App with createElement
const component = function(name, props, children) {
let elem = document.createElement(name);
Object.keys(props).forEach(key => {
if (key === 'style') {
Object.assign(elem.style, props[key])
} else {
elem.setAttribute(key, props[key]);
// elem[key] = props[key];
}
});
var children = Array.prototype.slice.call(arguments, 2);
if (children && children.length > 0) {
children.forEach(child => {
if (typeof child === "string") {
child = document.createTextNode(child);
}
elem.appendChild(child);
});
};
return elem;
};
let App =
component('div', { className: 'app' },
component('div', { className: 'app__logo' }),
component('span', { className: 'app__text' },
'hello'
)
);
window.onload = () => {
document.body.appendChild(App);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment