hello the subject is already treated but I still do not find the true answer.
i'm new to react and I want to render a list of "name". I have used map function to loop through the object, but I I only have the first "name" (Library 1) and not others. Provide me the complete for-loop syntax to render the object. thanks
const side =[
{
"id": "LIB1",
"name": "Library 1",
"context": "C1",
"children": [
{
"id": "SKI1",
"name": "SKill 1",
"context": "C1",
"children": [
{
"id": "SKI11",
"name": "SKill 11",
"context": "C1"
},
{
"id": "SKI12",
"name": "SKill 12",
"context": "C1",
"children": []
},
{
"id": "SKI13",
"name": "SKill 13",
"context": "C1",
"children": [
{
"id": "SKI131",
"name": "SKill 131",
"context": "C1",
"children": [
{
"id": "SKI1311",
"name": "SKill 1311",
"context": "C1",
"children": [
{
"id": "SKI13111",
"name": "SKill 13111",
"context": "C1"
}
]
}
]
}
]
}
]
},
{
"id": "SKI2",
"name": "SKill 2",
"context": "C1",
"children": [
{
"id": "SKI21",
"name": "SKill 21",
"context": "C1",
"children": [
null
]
},
{
"id": "SKI22",
"name": "SKill 22",
"context": "C1"
}
]
}
]
}
]
// Rendu recursif de donnée json
const MyComponent = ({collection}) => {
if(Array.isArray(collection)) {
return <ul>
{collection.map((data)=> {
return (<ul>
<li>{data.name}</li>
<li><MyComponent collection={data.name}/></li>
</ul>)
})
}
</ul>
}
return null;
}
class App extends React.Component {
render() {
return (
<MyComponent collection={side}/>
)
}
}