Answer the question
In order to leave comments, you need to log in
How to recursively render nested arrays?
There is the following structure:
[{
"id": "market-executive-summary",
"title": "Executive Summary",
"interactivity": false,
"priceFactor": 2,
"children": [{
"id": "market-executive-summary-overview",
"title": "Overview",
"interactivity": false,
"priceFactor": 0,
"children": []
}, {
"id": "market-executive-summary-key-findings",
"title": "Key Findings",
"interactivity": false,
"priceFactor": 0,
"children": []
}, {
"id": "market-executive-summary-conclusions-implications",
"title": "Conclusions & Implications",
"interactivity": false,
"priceFactor": 0,
"children": []
}]
},
{
"id": "market-definitions-segmentation",
"title": "Definitions & Segmentation",
"interactivity": false,
"priceFactor": 2,
"children": [{
"id": "market-definitions-segmentation-definitions",
"title": "Definitions",
"interactivity": false,
"priceFactor": 0,
"children": []
}]
}
]
{tags?.map((tag: IPresetTag) => (
<PresetTags {...tag} />
))}
export const PresetTags = ({ title, interactivity, children }: IPresetTag) => {
return (
<Accordion title={title} interactivity={interactivity}>
<List>
{children.map((tag: IPresetTag) => (
<PresetTagContent button key={tag.id}>
<CheckMark />
<ListItemText>{tag.title}</ListItemText>
</PresetTagContent>
))}
</List>
</Accordion>
);
};
Answer the question
In order to leave comments, you need to log in
It is better to implement recursion directly on the components -
https://codesandbox.io/s/silly-ramanujan-3giij?fil...
import React from "react";
import "./styles.css";
const data = [{
"id": "market-executive-summary",
"title": "Executive Summary",
"interactivity": false,
"priceFactor": 2,
"children": [{
"id": "market-executive-summary-overview",
"title": "Overview",
"interactivity": false,
"priceFactor": 0,
"children": []
}, {
"id": "market-executive-summary-key-findings",
"title": "Key Findings",
"interactivity": false,
"priceFactor": 0,
"children": []
}, {
"id": "market-executive-summary-conclusions-implications",
"title": "Conclusions & Implications",
"interactivity": false,
"priceFactor": 0,
"children": []
}]
},
{
"id": "market-definitions-segmentation",
"title": "Definitions & Segmentation",
"interactivity": false,
"priceFactor": 2,
"children": [{
"id": "market-definitions-segmentation-definitions",
"title": "Definitions",
"interactivity": false,
"priceFactor": 0,
"children": []
}]
}
]
const List = ({data}) => {
let children = data.map(data => <Item {...data}/>)
return (
<ul id>
{children}
</ul>
);
}
const Item = ({id, title, children}) => {
let list = children.length ? <List data={children}/> : null;
return (
<li>
<span>{title}</span>
{list}
</li>
);
}
export default function App() {
return (
<List data={data}/>
);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question