Skip to content

Instantly share code, notes, and snippets.

@DanAlvares
Last active April 1, 2021 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanAlvares/3415a81505cbbc22a3d9011fa5fa1600 to your computer and use it in GitHub Desktop.
Save DanAlvares/3415a81505cbbc22a3d9011fa5fa1600 to your computer and use it in GitHub Desktop.
JSX function - Use HTML in .ts(x) files instead of template strings
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