README.md
md12 kBIn React_Labs_Chapters_05_07.zip
React Lab 02 - Product Catalog Application
🎯 Project Overview
An interactive product catalog application built with React that demonstrates:
- Props (Chapter 05): Component communication via props and prop destructuring
- Events (Chapter 06): User interactions through event handlers
- useState Hook (Chapter 07): State management for dynamic UI updates
🚀 Setup Instructions
-
Install Dependencies
npm install -
Start Development Server
npm run dev -
Open in Browser
- Navigate to
http://localhost:5173(or the URL shown in terminal)
- Navigate to
📦 Components Description
1. ProductCard (src/components/ProductCard.jsx)
Purpose: Displays individual product information with action buttons
Props Used:
id(number): Unique product identifiername(string): Product nameprice(number): Product pricedescription(string): Product descriptionimage(string): Product emoji/icononAddToCart(function): Callback function when "Add to Cart" is clickeddiscount(number, optional): Discount percentage for stretch goal
Key Features:
- Prop Destructuring: Uses destructuring in function parameters
- Function Props: Accepts and calls
onAddToCartcallback - Price Formatting: Formats prices as currency ($X.XX)
- Conditional Rendering: Shows discount pricing when available
- Event Handling: Click handler for "Add to Cart" button
Event Handlers:
onClick: TriggersonAddToCart(id)when button is clicked
2. CartSummary (src/components/CartSummary.jsx)
Purpose: Displays shopping cart summary and checkout functionality
Props Used:
count(number): Total number of items in carttotal(number): Total price of all cart itemsonCheckout(function): Callback function for checkout action
Key Features:
- Price Formatting: Displays total as currency
- Conditional Button State: Disables checkout button when cart is empty
- Dynamic Button Text: Changes text based on cart state
Event Handlers:
onClick: Triggers checkout process when button is clicked
3. SearchBar (src/components/SearchBar.jsx)
Purpose: Provides search functionality for filtering products
Props Used:
value(string): Current search query (controlled input)onChange(function): Handler for input changesplaceholder(string): Placeholder text for input
Key Features:
- Controlled Component: Value managed by parent component
- Multiple Event Handlers: Implements both
onChangeandonFocus - Console Logging: Logs when search input receives focus
Event Handlers:
onChange: Updates search query as user typesonFocus: Logs "Search focused" to console
4. AddProductForm (src/components/AddProductForm.jsx)
Purpose: Form for adding new products to the catalog
Props Used:
onSubmit(function): Callback function when form is submitted
Key Features:
- Form State Management: Uses
useStatewith object containing multiple fields - Controlled Inputs: All form fields are controlled components
- Spread Operator: Updates form state immutably using spread syntax
- Form Validation: Validates required fields and data types (stretch goal)
- Error Handling: Displays error messages for invalid inputs
- Form Reset: Clears form after successful submission
- Dynamic Emoji Assignment: Assigns category-appropriate emoji to products
Event Handlers:
onChange: Updates form state for each input fieldonSubmit: Validates and submits form data (withpreventDefault())
Form Fields:
- Name (text input)
- Price (number input)
- Description (textarea)
- Category (select dropdown)
🔄 State Variables & Their Purposes
In App.jsx
-
products(array of objects)- Initial Value:
INITIAL_PRODUCTSarray - Purpose: Stores all available products
- Updated By:
handleAddProductfunction when new products are added
- Initial Value:
-
cartItems(array of objects)- Initial Value:
[](empty array) - Purpose: Stores products that have been added to cart
- Updated By:
handleAddToCart: Adds products using functional updatehandleCheckout: Resets to empty array
- Initial Value:
-
cartCount(number)- Initial Value:
0 - Purpose: Tracks total number of items in cart
- Updated By:
handleAddToCart: Increments using functional updatehandleCheckout: Resets to 0handleKeyDown: Resets to 0 when Escape is pressed
- Initial Value:
-
showCart(boolean)- Initial Value:
false - Purpose: Controls visibility of CartSummary component
- Updated By:
handleToggleCartfunction toggles between true/false
- Initial Value:
-
searchQuery(string)- Initial Value:
""(empty string) - Purpose: Stores current search term for filtering products
- Updated By:
handleSearchChangefunction as user types
- Initial Value:
-
selectedCategory(string) - Stretch Goal- Initial Value:
"All" - Purpose: Filters products by category
- Updated By:
handleCategoryChangefunction
- Initial Value:
In AddProductForm.jsx
-
formData(object)- Initial Value:
{ name: '', price: '', description: '', category: '' } - Purpose: Manages all form field values as a single state object
- Updated By:
handleChange: Updates specific field using spread operatorhandleSubmit: Resets to initial state after submission
- Initial Value:
-
errors(object) - Stretch Goal- Initial Value:
{} - Purpose: Stores validation error messages for each field
- Updated By:
validateForm: Sets errors for invalid fieldshandleChange: Clears errors as user corrects input
- Initial Value:
🎯 Key Concepts Demonstrated
Chapter 05: Props
- ✅ Prop destructuring in component parameters
- ✅ Passing data from parent to child components
- ✅ Function props (callbacks)
- ✅ Optional props (discount in ProductCard)
- ✅ Default prop values (placeholder in SearchBar)
Chapter 06: Events
- ✅
onClickevent handlers for buttons - ✅
onChangeevent handlers for inputs - ✅
onFocusevent handlers for focus tracking - ✅
onSubmitevent handlers for forms - ✅
onKeyDownevent handlers for keyboard shortcuts - ✅
preventDefault()to prevent default form submission - ✅ Event object usage (e.target, e.key, etc.)
Chapter 07: useState Hook
- ✅ Multiple state variables in single component
- ✅ State as primitives (numbers, strings, booleans)
- ✅ State as complex objects (arrays, objects)
- ✅ Functional updates (
setState(prev => ...)) - ✅ Controlled components (form inputs)
- ✅ Conditional rendering based on state
- ✅ State management for forms as objects
- ✅ Derived state (filtered products, cart total)
✨ Stretch Goals Completed
Part 1: ProductCard Enhancements
- ✅ Optional Discount Prop: Displays original price with strikethrough and discounted price in green
- Laptop has 10% discount
- Headphones have 15% discount
Part 2: Keyboard Support
- ✅ Escape Key Handler: Press Escape to clear cart (with confirmation dialog)
- ✅ Attached to main layout with
onKeyDownevent
Part 3: Category Filtering
- ✅ Category State:
selectedCategorystate variable - ✅ Category Buttons: Dynamic buttons for each category (All, Electronics, Audio, Books)
- ✅ Functional Updates: Uses
setState(prev => ...)for toggling - ✅ Visual Feedback: Active category button highlighted in blue
Part 4: Form Validation
- ✅ Required Field Validation: Name, price, and category are required
- ✅ Price Validation: Must be a valid number greater than 0
- ✅ Error Messages: Red error text displays below invalid fields
- ✅ Real-time Error Clearing: Errors clear as user corrects input
- ✅ Prevents Invalid Submission: Form won't submit until all validations pass
🛠️ Additional Features
User Experience Enhancements
- Empty State Handling: Shows message when no products match filters
- Disabled States: Checkout button disabled when cart is empty
- Loading Feedback: Console logs for debugging and tracking
- Confirmation Dialogs: Asks user before clearing cart
- Success Messages: Alerts after successful product addition or checkout
- Product Counter: Shows number of filtered products
UI Polish
- Responsive Design: Grid layout adapts to screen size
- Hover Effects: Interactive feedback on buttons and cards
- Visual Hierarchy: Clear section separation with cards
- Icon Usage: Emojis for visual appeal and category identification
- Dynamic Styling: Category buttons change color when active
- Keyboard Shortcut Hint: Footer shows Escape key instruction
📁 Project Structure
lab2-solution/
├── src/
│ ├── components/
│ │ ├── ProductCard.jsx # Product display component
│ │ ├── CartSummary.jsx # Shopping cart display
│ │ ├── SearchBar.jsx # Search input component
│ │ └── AddProductForm.jsx # New product form
│ ├── styles/
│ │ └── lab-theme.css # Predefined styling
│ ├── App.jsx # Main application component
│ └── main.jsx # Application entry point
├── index.html
├── package.json
├── vite.config.js
└── README.md
🧪 Testing Checklist
- Products display correctly in grid layout
- Search filters products by name and description (case-insensitive)
- Category filter buttons work correctly
- Add to Cart button adds items and updates counter
- Cart summary shows correct count and total price
- Cart toggles visibility when button is clicked
- Checkout displays summary and clears cart
- Checkout button disabled when cart is empty
- Add Product Form validates all required fields
- Add Product Form shows appropriate error messages
- New products appear in catalog immediately
- Discount prices display correctly with strikethrough
- Search input logs "Search focused" to console
- Escape key clears cart (with confirmation)
- All event handlers work without errors
- Application runs without console errors
🎓 Learning Outcomes
Through this lab, I learned:
- Props Management: How to effectively pass data and functions between components
- Event Handling: Implementing various event handlers for user interactions
- State Management: Managing complex state with multiple useState hooks
- Functional Updates: Using previous state to calculate new state safely
- Controlled Components: Synchronizing form inputs with React state
- Form Validation: Implementing client-side validation with error handling
- Conditional Rendering: Showing/hiding components based on state
- Array Operations: Filtering, mapping, and reducing arrays for derived state
- Component Composition: Building a complete app from smaller, reusable components
📝 Notes
- All styling uses the provided
lab-theme.css - No external state management libraries used (only useState)
- No useEffect or other advanced hooks used (per lab requirements)
- All components are functional components
- Event handlers use arrow functions to maintain
thiscontext - Functional updates used when new state depends on previous state
🚀 Future Enhancements
Potential improvements for future iterations:
- Persist cart to localStorage
- Add product quantity controls
- Implement product ratings and reviews
- Add product images (not just emojis)
- Create detailed product view modal
- Implement sorting options (price, name, etc.)
- Add shopping cart item removal
- Create order history feature