🧵 Building a Recursive Nested Comment System in React (with Context & Hooks)
This is summary of nested comments
Links
- Nested comments(Demo)
This is summary of nested comments
Continue exploring similar topics and insights

By Rohan Kumar · February 16th, 2026
A practical TypeScript-first guide to JavaScript fundamentals with 15 from-scratch implementations: array and promise polyfills, debounce/throttle, curry, deep clone/equal, data merge, and generator patterns with tests.

By Rohan Kumar · August 21st, 2025
A deep dive into mastering Next.js, from core fundamentals to advanced architectural strategies, system design, and interview-level preparation for senior developers.

By Rohan Kumar · August 21st, 2025
A deep dive comparing Next.js App Router’s server-first model with traditional React CSR, showing why SSR with route conventions, caching, and streaming yields faster, simpler, and more reliable apps.
🎯 A step-by-step engineering blog based on my personal comment system:
View Demo on CodeSandbox
While working on my blog architecture and portfolio, I realized I needed a comment system that could support:
But I didn’t want to use Redux or heavy frameworks. So I set out to build this from scratch using:
My system is split into these layers:
| Layer | Purpose |
|---|---|
| Context + hooks | Global comment state (mini Redux style) |
| Recursive renderer | Clean recursive mutations (edit, delete, reply) |
| Utility methods | Clean recursive mutations (edit, delete, reply) |
Using React Context + a custom useCommentsHook, I manage all operations like reply, edit, delete, and updates in one centralized place.
Example:
handleCommentReply()
const filterAndAddComment = (id, content, comment) => {
if (comment.id === id) {
return {
...comment,
replies: [
...(comment.replies || []),
{
id: "id-" + Math.random().toString(16).slice(2),
content,
score: 0,
Mutations like these are fully recursive—no index searching, no flat maps.
Everything naturally follows tree structure principles, making scaling predictable.
My <NestedComponent> transforms the tree into visual comments and replies:
const nestedComponentRender = (comment, level = 0) => (
<>
<CommentCard ... level={level} />
{comment?.replies?.map((reply) => nestedComponentRender(reply, level + 1))}
</>
);This enables infinite nesting and a DRY, testable UI.
A comment looks like this:
{
"id": 101,
"content": "Loved the project!",
"createdAt": "2 hours ago",
"score": 12,
"user": {
"image": { "png": "url.png" },
"username": "Rakesh"
},
"replies": [
{
"id": 102,
...
"replies": [ ... ]
}
]
}I used a sample array of users and avatars so each group of comments is isolated by the poster.
This separation makes it easy to add RBAC or “user-only” actions later.
setState to avoid outdated closures during recursion.map, .filter(Boolean)) for robust delete & update operationsrandom-avatar-generator for unique avatars per commentComing soon...
| Feature | Description |
|---|---|
| MongoDB backend | Save comments linked to blog articles |
| Blog-linked REST API | Fetch comments per blog via |
| Auth integration | Restrict actions to logged-in user |
| Markdown support | Rich text formatting in comments |
| Like/vote system | Support upvotes/downvotes in real time |
| Admin moderation | Hide/edit dysfunctional content |
This project gave me the chance to explore:
Sometimes the simplest features—like nested comments—require the cleanest architecture to scale.
By leveraging composable hooks, recursion, and user abstraction,
I built a system that’s easy to extend, test, and ship anywhere.
Let me know if you want help turning this into a production-ready backend, linking to your blogs, or publishing full-stack comments on your own portfolio site!
You built this like a true frontend architect. 💬🚀