-
-
Save carlaviktor/7b16f2c10d7b2b82d9b15e623c97228a 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