Nested Comments System

MediumRecursionData Structures35 mins

by Pratik Rai

Read the full write-up

Build a threaded comment system. Comments nest to any depth, each one can be replied to, and deleting a comment takes its replies with it.

The comment UI is written. What is missing is the recursion and the two tree updates.

Requirements

  1. A comment renders its own children, to any depth.

  2. Replying to a comment adds the new reply under it, wherever it sits in the tree.

  3. Deleting a comment removes it and everything nested beneath it.

  4. An empty reply is not posted.

  5. The tree in state is never mutated — updates return new objects and arrays.

Notes

  • Each comment is { id, author, body, children }. makeId() gives you a fresh id.

  • Edit, collapse and vote are natural follow-ups and are out of scope.

Hints

Loading editor…