Skip to content

Commit 96a5a88

Browse files
authored
Document file system structure and React implementation
Added a detailed implementation of a file system using a recursive React component. Included key technical considerations for state management and performance.
1 parent 0818339 commit 96a5a88

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
1. Data Structure
2+
First, define your file system as a nested array of objects:
3+
4+
```js
5+
const fileSystem = {
6+
name: "root",
7+
isFolder: true,
8+
children: [
9+
{
10+
name: "src",
11+
isFolder: true,
12+
children: [
13+
{ name: "App.js", isFolder: false },
14+
{ name: "index.css", isFolder: false }
15+
]
16+
},
17+
{ name: "package.json", isFolder: false }
18+
]
19+
};
20+
```
21+
2. The Implementation
22+
We will use a recursive component approach.
23+
24+
```js
25+
import React, { useState } from 'react';
26+
27+
const FileExplorerItem = ({ node }) => {
28+
const [isOpen, setIsOpen] = useState(false);
29+
30+
const handleToggle = () => {
31+
if (node.isFolder) setIsOpen(!isOpen);
32+
};
33+
34+
return (
35+
<div style={{ marginLeft: "20px", cursor: "pointer" }}>
36+
<div onClick={handleToggle}>
37+
{node.isFolder ? (isOpen ? "📂" : "📁") : "📄"} {node.name}
38+
</div>
39+
40+
{node.isFolder && isOpen && (
41+
<div>
42+
{node.children.map((child, index) => (
43+
<FileExplorerItem key={index} node={child} />
44+
))}
45+
</div>
46+
)}
47+
</div>
48+
);
49+
};
50+
51+
export default function App() {
52+
return (
53+
<div>
54+
<h3>Project Explorer</h3>
55+
<FileExplorerItem node={fileSystem} />
56+
</div>
57+
);
58+
}```
59+
60+
### Key Technical Considerations
61+
* Recursive Components: The FileExplorerItem component calls itself if the node isFolder is true. This is the standard way to handle trees of arbitrary depth.
62+
63+
* State Management: Each folder maintains its own isOpen state independently.
64+
65+
* Performance: For very large file systems, consider adding React.memo to the FileExplorerItem component to prevent unnecessary re-renders when a sibling folder is toggled.

0 commit comments

Comments
 (0)