call(back)
FrontendmediumSign in to star problems

Nested Comment Thread

Build a comment thread like the one under a video. data.js exports initialComments, a flat list of { id, parentId, author, text } in which a parentId of null marks a top-level comment.

Requirements

  • Nesting. Render the top-level comments in the list labelled Comments. Each comment is an <li> showing its author and text, and its replies are rendered in a nested list inside that <li>, to any depth, in the order they appear in the data.
  • Reply. Every comment has a Reply button. Clicking it shows a text box labelled Reply to <author> with Post and Cancel buttons. Post is disabled while the box is empty or only spaces. Posting adds a reply by You under that comment and closes the form; Cancel closes it without adding anything.
  • Hide and show. A comment with replies has a button reading Hide replies (n), where n is the number of direct replies. Clicking it removes the replies from the page and the button becomes Show replies (n).
  • New comment. The form at the top, with the box labelled Add a comment and the Comment button, adds a top-level comment by You at the end and clears the box.

Keep the starter's class names and labels. Be ready to discuss how the component's API would look if another team reused it, and what changes with ten thousand comments.

Hints

Hint 1

Keep the comments flat in state, as they arrive. Group them by parentId once per change and let a recursive Comment component look up its own replies.

Hint 2

The open reply form, its draft text and the collapsed flag are local to one comment; they belong in that Comment's own state, not in the list.

Hint 3

Adding a reply is an append to the flat list with the right parentId. No deep update of a nested structure is needed.

Asked at

Similar Problems6