Frontend School

Dynamic Tree View with Lazy Loading

Machine Coding
hard

Dynamic Tree View with Lazy Loading

Hard
React
TypeScript
UI
Tree View
Recursion
State Management
Lazy Loading

Overview

In modern applications, navigating complex hierarchies of data is a common requirement. Imagine you're building a file explorer, a resource manager, or an organizational chart. A robust and performant Tree View component is crucial for users to efficiently browse, understand, and interact with structured information. This problem challenges you to build a dynamic Tree View that can handle large datasets gracefully.

What you need to do / Task

Your primary task is to implement a reusable React Tree View component in TypeScript that displays hierarchical data. The component should be capable of:

  1. Recursive Rendering: Display a tree structure based on provided hierarchical data.
  2. Expand/Collapse Functionality: Allow users to expand and collapse folder nodes to reveal or hide their children.
  3. Single Node Selection: Enable users to select a single node at a time. The selected node should be visually highlighted.
  4. Lazy Loading of Children ("Load More"): For folders that contain many children, initially display only a subset. A "Load More" button should appear, allowing the user to incrementally load additional chunks of children for that specific folder. This should simulate an asynchronous operation.

UI/UX expectations

  • Hierarchy: Use indentation to clearly represent the parent-child relationships between nodes.
  • Expand/Collapse Indicators: Display a clear visual indicator (e.g., > for collapsed, v for expanded) next to folder nodes.
  • Selection Highlight: The currently selected node should have a distinct background color or border.
  • Node Types: Differentiate visually between 'file' and 'folder' nodes (e.g., using icons or different text styles).
  • "Load More" Button: The "Load More" button should be clearly visible when applicable, positioned appropriately within the folder node it pertains to. It should disable itself briefly during the simulated loading process.

Acceptance criteria

  • The tree renders correctly with initial data and proper indentation.
  • Clicking a folder's expand/collapse indicator toggles its children's visibility.
  • Clicking any node selects it, and only one node is selected at a time.
  • Selected node is visually highlighted.
  • Folder nodes with more children than initially displayed show a "Load More" button.
  • Clicking "Load More" appends the next chunk of children to that specific folder.
  • The "Load More" button disappears when all children for that folder have been loaded.
  • The component handles nested "Load More" scenarios (i.e., a loaded child folder might itself have a "Load More" button).
  • The solution uses React and TypeScript effectively, demonstrating good component design and state management.

Constraints

Constraints & assumptions

  • You are building this component using React and TypeScript.
  • The data structure for nodes will be hierarchical as defined in /App.tsx.
  • The "Load More" functionality should simulate asynchronous loading using setTimeout (e.g., 200-300ms delay) to mimic network latency.
  • Each "load more" operation should append a fixed CHUNK_SIZE of children (e.g., 5 items).
  • Performance should be considered for potentially deep or wide trees, especially regarding re-renders.
  • The solution should be contained within the provided Sandpack environment. No external UI libraries (e.g., Material UI, Ant Design) are allowed for the tree view itself; basic styling should be done via CSS.

Out of scope

  • Drag-and-drop functionality for nodes.
  • Context menus or advanced node interactions beyond expand/collapse and selection.
  • Keyboard navigation beyond basic focus.
  • Full accessibility implementation (e.g., ARIA roles, advanced screen reader support) beyond semantic HTML.
  • Complex search, filtering, or sorting of tree nodes.
  • Persistent state (e.g., saving expanded state across page loads).

What interviewers evaluate

  • Component Design & Structure: Clarity, reusability, and modularity of React components, especially for the recursive nature of the tree.
  • State Management: Effective use of React hooks (useState, useEffect, useCallback) to manage component state (expansion, selection, dynamically loaded children) and ensure efficient updates.
  • Algorithm & Logic: Correct implementation of the recursive tree rendering and the "Load More" lazy-loading mechanism, including handling edge cases.
  • UI/UX Fidelity: Adherence to the specified UI/UX requirements for visual hierarchy, expand/collapse indicators, selection highlighting, and "Load More" button behavior.

Loading editor...