Created
February 12, 2018 02:01
-
-
Save christianscott/f7a853bcdc83ad5e45920b3b9f9a5f85 to your computer and use it in GitHub Desktop.
JS Function calls to html
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
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