Last active
February 1, 2022 14:15
-
-
Save UsmanHaider15/650b5d09b658ebac160d4e0ff6d51295 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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