Last active
January 2, 2020 21:35
-
-
Save tripolskypetr/0b6a671e2b551631a9a8686081fb0feb to your computer and use it in GitHub Desktop.
Custom JSX factory https://jsfiddle.net/tripolskypetr/7yfbxL29/
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
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