Created
September 19, 2017 14:47
-
-
Save danielstocks/874b04775eb9574c7f282e1a0abee521 to your computer and use it in GitHub Desktop.
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
const testPageJSON = { | |
"children": [ | |
{ | |
"el": "div", | |
"props": { | |
"style": { | |
"background": "green" | |
} | |
}, | |
"children": [ | |
{ | |
"el": "h1", | |
"props": { | |
"style": { | |
"color": "red" | |
} | |
}, | |
"children": [ | |
"The quick brown fox jumped over the lazy dog" | |
] | |
}, | |
{ | |
"el": "h2", | |
"children": [ | |
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua" | |
] | |
} | |
] | |
} | |
] | |
} | |
class App extends Component { | |
render() { | |
return ( | |
<div className='root' style={{'background': 'yellow'}}> | |
{testPageJSON.children.map(child => ( | |
<RecursiveNode node={child} /> | |
))} | |
</div> | |
); | |
} | |
} | |
class RecursiveNode extends Component { | |
render() { | |
const node = this.props.node | |
if(typeof(node) === 'string') { | |
return <span>{node}</span> | |
} | |
const el = React.createElement( | |
node.el, | |
node.props, | |
node.children.map(child => ( | |
<RecursiveNode node={child} /> | |
)) | |
) | |
return el | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment