Skip to content

Instantly share code, notes, and snippets.

@namelos
Created February 16, 2016 05:49
Show Gist options
  • Select an option

  • Save namelos/e209397c4201dacec710 to your computer and use it in GitHub Desktop.

Select an option

Save namelos/e209397c4201dacec710 to your computer and use it in GitHub Desktop.
react recursive rendering
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