Count Replies in a Comment Thread

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

The Thread component gets a comments prop: a list of { id, author, text, replies } objects, where replies is a list of comments in the same shape (it may be missing or empty), so replies nest to any depth. Render them recursively, each reply under its parent: the author's name, then the comment's text in its own element, such as a paragraph. At the top, a heading counts every comment at every depth, like "5 comments" ("1 comment" for exactly one). Each comment that has replies gets a button counting all the replies below it at any depth, like "Hide 3 replies" ("Hide 1 reply" for one). Replies start shown. Clicking the button hides all of them (leave them out, or hide them with the hidden attribute) and it then reads "Show 3 replies"; clicking it again shows them. Comments without replies get no button. Work the counts out from the props.

Your code must pass

  • Shows every comment and a heading like "5 comments"
  • Each comment with replies has a button counting every reply below it
  • Hiding a thread hides all its replies, and the button reads "Show 3 replies"
  • Show brings replies back, and each thread hides on its own