Skip to content

Instantly share code, notes, and snippets.

@tripolskypetr
Last active January 2, 2020 21:35
Show Gist options
  • Save tripolskypetr/0b6a671e2b551631a9a8686081fb0feb to your computer and use it in GitHub Desktop.
Save tripolskypetr/0b6a671e2b551631a9a8686081fb0feb to your computer and use it in GitHub Desktop.
class JSX {
static _indent = 0;
static createElement(type = '', props = null, ...child) {
return {
type,
props,
child
};
}
static render({type, props, child}) {
const print = (type, props) => {
const indent = [...new Array(JSX._indent)].map((c) => ' ').join('');
console.log(indent, type, JSON.stringify(props));
}
print(type, props);
JSX._indent += 2;
child.flat(Infinity).forEach((child) => JSX.render(child));
JSX._indent -= 2;
}
}
JSX.render(JSX.createElement('body', null,
JSX.createElement('p', {text: 'Hello'}),
JSX.createElement('p', {text: 'World'}),
JSX.createElement('div', null,
[...new Array(3)].map((_, index) => JSX.createElement('p', {text: index}))
),
JSX.createElement('div', null,
[...new Array(3)].map((_, i) => {
return [...new Array(3)].map((_, j) => JSX.createElement('tag', {i, j}))
})
),
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment