Skip to content

Instantly share code, notes, and snippets.

@UsmanHaider15
Last active February 1, 2022 14:15
Show Gist options
  • Save UsmanHaider15/650b5d09b658ebac160d4e0ff6d51295 to your computer and use it in GitHub Desktop.
Save UsmanHaider15/650b5d09b658ebac160d4e0ff6d51295 to your computer and use it in GitHub Desktop.
import React from "react";
const listData = [
"First Item",
"Second Item",
"Third Item",
{ "Fourth Item": ["First Nested Item", "Second Nested Item"] },
"Fifth Nested Item",
];
function App() {
return <RecursiveList listData={listData} />;
}
const RecursiveList = ({ listData }) => {
return (
<ul>
{listData.map((item) => {
if (typeof item === "string") {
return <li>{item}</li>;
} else {
const [[listItem, subItems]] = Object.entries(item);
return (
<li>
{listItem}
<RecursiveList listData={subItems} />
</li>
);
}
})}
</ul>
);
};
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment