@DanceMonkeyTime

Как рекурсивно отрендерить вложенные массивы?

Есть следущая структура:
[{
        "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": []
        }]
    }
]

Мне нужно рекурсивно рендерить 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>
  );
};
  • Вопрос задан
  • 65 просмотров
Решения вопроса 1
@twoone
Лучше рекурсию прямо на компонентах реализовать -

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}/>
  );
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы