Нужно cделать так, чтобы каждый элемент массива рендерился в формате компонента с текстом из
title и
text, дочерние элементы по той же логике из значения массивов items. Сложность в том, что при маппинге, у перебираемых объектов нет
children (undefined) и я просто не могу передать вложенность. Все решения, которые я нашел, основаны на рекурсии с передачей
children.
const Items = ({items}) => {
const items = [
{
title: 'Elements',
items: [
{
title: '1',
text: 'This is First one.',
items: [
{
title: '1:1',
text: 'This is First of the First one.',
items: [
{ title: '1:1:1', text: 'This is First of the First of the First one.' },
{ title: '1:1:2', text: 'This is Second of the First of the First one.', }
]
},
{
title: '1:2',
text: 'This is Second of the First one.',
items: [
{ title: '1:2:1', text: 'This is First of the Second of the First one.' },
{ title: '1:2:2', text: 'This is Second of the Second of the First one.' },
{ title: '1:2:3', text: 'This is Third of the Second of the First one.' },
]
},
]
},
{
title: '2',
text: 'This is Second one.',
items: [
{
title: '2:1',
text: 'This is First of the Second one.',
items: [
{ title: '2:1:1', text: 'This is First of the First of the Second one.' },
{ title: '2:1:2', text: 'This is Second of the First of the Second one.' }
]
}
]
},
{
title: '3',
text: 'This is Third one.'
}
]
}
];
return (
<>
{
items.map((item) => {
if (typeof(item) === Object) {
return (
<Items items={item.children}/>
)
}
return (
<Item item={item} />
)
})
}
</>
)
}
const Item = ({item}) => {
return (
<>
<h2>{item.title}</h2>
<p>{item.text}</p>
</>
)
}