Facebook Pixel

File Explorer Component in React

Building a recursive file explorer in React with expand/collapse state.

File Explorer Component in React

Building a recursive file explorer in React with expand/collapse state.

Component

import React, { useState } from "react"; function TreeNode({ node, depth }) { const [expanded, setExpanded] = useState(false); const isFolder = node.type === "folder"; return ( <div> <div className={`tree-node ${node.type}`} style={{ paddingLeft: depth * 20 }} onClick={(e) => { e.stopPropagation(); if (isFolder) setExpanded(!expanded); }} > <span className="icon">{isFolder ? (expanded ? "📂" : "📁") : "📄"}</span> <span>{node.name}</span> </div> {isFolder && expanded && node.children && ( <div className="children"> {node.children.map((child, i) => ( <TreeNode key={i} node={child} depth={depth + 1} /> ))} </div> )} </div> ); } function FileExplorer({ tree }) { return ( <div className="file-explorer"> <TreeNode node={tree} depth={0} /> </div> ); }

Features

  • Each TreeNode manages its own expanded state with useState.
  • Recursive rendering: TreeNode renders its children as TreeNode components.
  • Click toggles expand/collapse for folders.
  • Icon changes based on expanded state.
  • Indentation via paddingLeft: depth * 20.

CSS

.tree-node { padding: 4px 8px; display: flex; align-items: center; gap: 6px; cursor: pointer; } .tree-node:hover { background: #e0e0e0; } .tree-node.folder { font-weight: bold; }

The Takeaway

React file explorer: TreeNode component with useState for expand/collapse. Recursive rendering (TreeNode renders TreeNode for children). Click toggles expanded. Icon changes based on state. Indentation by depth. Each node manages its own state, making it simple and modular.

Create a TreeNode component with useState for expanded/collapsed. Render children recursively as TreeNode components. Click toggles expanded for folders. Icon changes based on state. Indentation via paddingLeft: depth * 20.

Use useState in each TreeNode: const [expanded, setExpanded] = useState(false). On click, setExpanded(!expanded). Only render children if expanded is true. Each node manages its own state independently.

The TreeNode component renders its children as TreeNode components: node.children.map(child => <TreeNode node={child} depth={depth + 1} />). This recursion renders the entire tree. Each node is a separate component with its own state.

Pass a depth prop to each TreeNode. Set style={{ paddingLeft: depth * 20 }}. Each level of nesting increases depth by 1, adding 20px of indentation. This creates the visual hierarchy.

Independence. Each node manages its own expand/collapse state, so toggling one node does not affect others. This is simpler than managing all state in a parent component. It also means React only re-renders the toggled node, not the entire tree.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.