I'm using the code below, and it does the job (for the moment). Depending on the length of the array, components are nested into each other with array-element reference as a prop. Inside the innermost element there must always be injected another element.
const arr = ['one', 'two', 'three'];
const injectedContent = <SomeContent />;
const renderNestedSections = () => {
switch (arr.length) {
case 1:
return (
<Section name={arr[0]}>
{injectedContent}
<Section>
);
case 2:
return (
<Section name={arr[0]}>
<Section name={arr[1]}>
{injectedContent}
</Section>
</Section>
);
case 3:
return (
<Section name={arr[0]}>
<Section name={arr[1]}>
<Section name={arr[2]}>
{injectedContent}
</Section>
</Section>
</Section>
);
default:
return null;
}
}
I'm struggling to make a function with algorithm that does the nesting dynamically. Any help will be much appreciated. Thanks in advance.