Nested Tabs From a Flat Array

MediumRecursionTreesComponentsReact30 mins

by Pratik Rai

Read the full write-up

You are given a flat array of items, each with an id, a name and a parentId.

Render them as a nested structure: every item appears under its parent, at any depth. Selecting one shows its panel.

Requirements

  1. Build a tree from the flat array — a parentId of null means a top-level item.

  2. Render children nested under their parent, to arbitrary depth.

  3. Selecting an item highlights it and updates the panel below.

  4. Items with children can be expanded and collapsed; leaves cannot.

  5. Each label carries role="tab" with aria-selected, and each twisty carries aria-expanded.

Notes

  • The data is deliberately more than two levels deep: Caching sits under Data fetching, which sits under Guides.

  • The styling is written — .level-nested handles indentation and .label-active the selected state.

Hints

Loading editor…