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
Replybutton. Clicking it shows a text box labelledReply to <author>withPostandCancelbuttons.Postis disabled while the box is empty or only spaces. Posting adds a reply byYouunder that comment and closes the form;Cancelcloses it without adding anything. - Hide and show. A comment with replies has a button reading
Hide replies (n), wherenis the number of direct replies. Clicking it removes the replies from the page and the button becomesShow replies (n). - New comment. The form at the top, with the box labelled
Add a commentand theCommentbutton, adds a top-level comment byYouat 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
Sign in to use the assistant — ask what the problem means, get a hint when you're stuck, or have your current work reviewed. Conversations save to your account.