Skip to content

Instantly share code, notes, and snippets.

@christianscott
Created February 12, 2018 02:01
Show Gist options
  • Save christianscott/f7a853bcdc83ad5e45920b3b9f9a5f85 to your computer and use it in GitHub Desktop.
Save christianscott/f7a853bcdc83ad5e45920b3b9f9a5f85 to your computer and use it in GitHub Desktop.
JS Function calls to html
function createElement(tag, props, ...children) {
return (
`<${tag}${renderProps(props)}>${children.join ? children.join('') : children}</${tag}>`
)
}
function renderProps(props) {
const propsString = Object.keys(props).map(key => `${key}="${props[key]}"`).join(' ')
return propsString ? ' ' + propsString : ''
}
function createFactory(tag) {
return (props, ...children) => createElement(tag, props, ...children)
}
const article = createFactory('article')
const h1 = createFactory('h1')
const p = createFactory('p')
const div = createFactory('div')
console.log(
article(
{
class: 'Article',
'data-title': 'Hello world'
},
h1({ class: 'Article__title' }, 'hello, world!'),
p({}, 'This is an article')
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment