This project is a simple comment widget built using React and Vite, with TailwindCSS for styling. It uses ContextAPI to manage state for comments and replies, avoiding props drilling for the replyComment feature.
Add, edit, and delete comments.
Reply to comments.
State management using Context API.
Follow these instructions to set up the project on your local machine.
Node.js (Ensure you have a version installed that works with Vite)
Vite
TailwindCSS
npm create vite@latest comment-widget -- --template react
cd comment-widget
npm install
npm install -D tailwindcss postcss autoprefixer
npm start
npm run build
Now, your project is set up to handle comments and replies efficiently with the help of ContextAPI, avoiding prop drilling issues.
-
Create
CommentContext.jsx
in the src folder. -
CommentProvider is a react component that uses useState to manage comments and provide functions for
edit, delete, add , reply
to comments.handleAddComment
: Adds a new comment to the state and updates local storage.handleEditComment
: Edits an existing comment based on its index.handleDeleteComment
: Deletes a comment by filtering it out of the state using index.handleReply
: Adds a reply to a specific comment.
- This component displays the comment input box and the list of comments.
useContext(CommentContext)
: This hook allows you to access the context value (comments and functions) provided by the CommentProvider.State Management
: It maintains the state for the comment input (inputComment), button text (commentBtnText), and the index of the comment being edited (editIndex).Rendering
: It maps over the comments from the context and renders the PostedComments component for each comment.
- This component displays individual comments and provides options to edit, delete, and reply.
Reply Functionality
: It shows buttons to reply, edit, and delete a comment. It also toggles the reply input visibility.Rendering Replies
: If there are replies, they are displayed below the comment.
- This component handles the input for replies to comments.
reply Management
: It allows users to input a reply and sends it to the context for state management.State Management
: It uses useState to manage the value of the reply input.
- Finally, Wrap you entire application(part of it) in the
CommentProvider
to make comments state and functions available throughout you component tree.