Created
February 16, 2016 05:49
-
-
Save namelos/e209397c4201dacec710 to your computer and use it in GitHub Desktop.
react recursive rendering
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
| import React from 'react' | |
| const Node = ({ data }) => { | |
| if (data instanceof Array) | |
| return <ol>{ data.map(i => <Node data={ data[i - 1] } key={ i } />) }</ol> | |
| else if (data instanceof Object) | |
| return <ul>{ Object.keys(data).map(i =><Node data={ data[i] } key={ i } />) }</ul> | |
| else | |
| return <li>{ data }</li> | |
| } | |
| const data = { | |
| a: { | |
| aa: 1, | |
| ab: 2, | |
| ac: 3 | |
| }, | |
| b: { | |
| ba: { | |
| baa: 1, | |
| bab: 2 | |
| }, | |
| bb: [1, 2, 3] | |
| }, | |
| c: [1, 2, 3, 4], | |
| d: 1 | |
| } | |
| export const Usage = () => <Node data={ data } /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment