August 30, 2021

🎯 Goal: A Small Shopping Cart Page

We’ll create:

  1. App: Main component
  2. ProductList: Shows a list of products
  3. ProductItem: One single product
  4. Cart: Shows added items
  5. Use props to pass data, and hooks like useState and useEffect

🧱 1. App.js – Main Component

import React, { useState } from "react";
import ProductList from "./ProductList";
import Cart from "./Cart";

function App() {
  const [cartItems, setCartItems] = useState([]);

  const handleAddToCart = (product) => {
    setCartItems((prevItems) => [...prevItems, product]);
  };

  return (
    <div>
      <h1>🐢 Pet Store</h1>
      <ProductList onAddToCart={handleAddToCart} />
      <Cart items={cartItems} />
    </div>
  );
}

export default App;

βœ… What you learned:

  • cartItems is state (useState)
  • We pass the function handleAddToCart as a prop to child
  • We pass data cartItems to Cart as a prop

πŸ›’ 2. ProductList.js – List of Products

import React from "react";
import ProductItem from "./ProductItem";

const products = [
  { id: 1, name: "Cat Toy", price: 10 },
  { id: 2, name: "Dog Food", price: 25 },
  { id: 3, name: "Bird Cage", price: 40 }
];

function ProductList({ onAddToCart }) {
  return (
    <div>
      <h2>πŸ› Products</h2>
      {products.map((product) => (
        <ProductItem key={product.id} product={product} onAddToCart={onAddToCart} />
      ))}
    </div>
  );
}

export default ProductList;

βœ… What you learned:

  • We pass product and onAddToCart as props to ProductItem

🧸 3. ProductItem.js – One Product

import React from "react";

function ProductItem({ product, onAddToCart }) {
  return (
    <div style={{ border: "1px solid gray", padding: "10px", margin: "5px" }}>
      <h3>{product.name}</h3>
      <p>Price: ${product.price}</p>
      <button onClick={() => onAddToCart(product)}>Add to Cart</button>
    </div>
  );
}

export default ProductItem;

βœ… What you learned:

  • product is a prop (an object with name & price)
  • We call onAddToCart(product) when clicking the button

πŸ› 4. Cart.js – Shopping Cart Component

import React, { useEffect } from "react";

function Cart({ items }) {
  useEffect(() => {
    console.log("πŸ›’ Cart updated!", items);
  }, [items]); // πŸ‘ˆ Watch the `items` change

  return (
    <div>
      <h2>πŸ›’ Cart</h2>
      {items.length === 0 && <p>No items in cart.</p>}
      <ul>
        {items.map((item, index) => (
          <li key={index}>{item.name} - ${item.price}</li>
        ))}
      </ul>
    </div>
  );
}

export default Cart;

βœ… What you learned:

  • useEffect(() => {}, [items]): Runs only when items change
  • Great for doing something like logging, fetching data, or analytics

πŸ”„ Hooks Used

HookWhy we use it
useStateTo manage the cart’s state
useEffectTo react to changes in cart items

πŸŽ“ Summary

ConceptExample
Propsproduct, onAddToCart, items
Pass functionPass handleAddToCart from parent to child
useStateStore the cart items
useEffectRun something when data (like items) changes
Multiple componentsApp, ProductList, ProductItem, Cart