Sophia Lee

How to implement nested comments (level 2) in React?

asked by Sophia Lee
react
state
ui

I want to allow users to comment on a post and reply to comments (one level deep). What's a simple state model?

Answers

David Kim
David Kim

Use a flat array grouped by parentId for comments and render replies by filtering where parentId === comment.id.

Maria Gonzalez
Maria Gonzalez
Accepted

Keep it simple: store comments in local state with a structure { id, content, parentId }. Render two passes.