Last active
April 1, 2021 15:33
-
-
Save DanAlvares/3415a81505cbbc22a3d9011fa5fa1600 to your computer and use it in GitHub Desktop.
JSX function - Use HTML in .ts(x) files instead of template strings
This file contains 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 JSX = { | |
createElement(name: string, props: { [id: string]: string }, ...content: string[]) { | |
props = props || {}; | |
const propsstr = Object.keys(props) | |
.map(key => { | |
const value = props[key]; | |
if (key === "className") return `class="${value}"`; | |
else return `${key}="${value}"`; | |
}) | |
.join(" "); | |
return `<${name} ${propsstr}> ${content.join("")}</${name}>`; | |
}, | |
}; | |
export default JSX; | |
/* USAGE: | |
import JSX from './jsx'; | |
const template = <div>Some HTML</div>; | |
document.querySelector('body').innerHTML = template; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment