Given an array of file objects, build a component that displays them in a hierarchical tree format.
There are two types of objects – files and directories:
You may assume that the IDs and names within the same directory are unique.
interface FileObject {
id: number;
name: string;
children?: FileObject[];
}
Example data:
const fileData = [
{
id: 1,
name: 'README.md',
},
{
id: 2,
name: 'Documents',
children: [
{
id: 3,
name: 'Word.doc',
},
{
id: 4,
name: 'Powerpoint.ppt',
},
],
},
];
File explorers are usually implemented in a tree-like navigation fashion and are hierarchical and recursive in nature.
In terms of front end components, recursion means that the components can be rendering itself, or render a component that renders itself. Hence it's important to design the recursive components well to allow for reusability.