HelenSobi/React-Comment-Widget
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
React Comment Widget with Reply, Edit, and Context API Implementation
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.
Features
Add, edit, and delete comments.
Reply to comments.
State management using Context API.
Getting Started
Follow these instructions to set up the project on your local machine.
Prerequisites
Node.js (Ensure you have a version installed that works with Vite)
Vite
TailwindCSS
1. Create a React Project Using Vite
npm create vite@latest comment-widget -- --template react
cd comment-widget
npm install
2. Install TailwindCSS and configure it
npm install -D tailwindcss postcss autoprefixer
3. Implement ContextAPI to Avoid Props Drilling
4. start the development server
npm start
5. build your application for production
npm run build
Now, your project is set up to handle comments and replies efficiently with the help of ContextAPI, avoiding prop drilling issues.
"Code Walkthrough":
1. Setting up the context
-
Create
CommentContext.jsxin the src folder. -
CommentProvider is a react component that uses useState to manage comments and provide functions for
edit, delete, add , replyto comments.Functions:
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.
2. CommentBox Component
- 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.
PostedComments Component
- 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.
ReplyComments Component
- 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.
Integrating the Provider
- Finally, Wrap you entire application(part of it) in the
CommentProviderto make comments state and functions available throughout you component tree.