Prices are one hundred times smaller than expected.
Likely cause: Rupee values were entered where paise are required.
Check and correct: Store 24900 for ₹249.00. Do not remove the division in the formatter to hide inconsistent data.
PROJECT 2 · LESSON 4 OF 18
Build a catalog from one data source and one reusable card.
Continue from lesson 3: Refactor cards and add interaction. Keep existing files; create or replace the complete files listed below.
Extract the ZIP, open its project folder, and run npm install. Database lessons also need the .env.local setup described below. Checkpoints never contain credentials or node_modules.
A real catalog changes more often than its card design. We want adding a product to mean adding data, not copying JSX. This lesson introduces a model, a reusable view and a page that joins them.
Product objects describe the data; ProductCard describes how one object appears. A catalog maps the array into cards. Adding data should not require editing the page markup.
Prices use integer paise to avoid introducing rounding errors in ordinary addition. Formatting for display happens at the edge of the UI.
Product describes the fields one card and one details page need: id, slug, name, price, category and description. id is a stable identity for application operations. slug is a human-readable URL segment. The name is display text and can change without requiring the ID to change.
TypeScript checks code against this shape while developing. It does not automatically validate external JSON or a user request at runtime. Our hard-coded array is trusted source data for this stage; later lessons add validation at network and storage boundaries.
products.map calls the rendering function once per object and returns an array of ProductCard elements. Each receives one product prop. ProductCard does not need to know how many products exist or where the list came from. The page handles the collection; the card handles one item.
The key helps React identify siblings when records are inserted, removed or reordered. It is not a visible product number and is not passed as a normal prop to the card. A stable product ID is appropriate. An array position can identify the wrong item after sorting.
The notebook’s price is 24900 paise, equivalent to ₹249.00. Two notebooks cost 49800 paise. Keeping calculations in integer minor units avoids the common floating-point surprises of repeatedly adding fractional currency amounts.
money divides by 100 only when formatting the result for display with Intl.NumberFormat. Keep the stored number separate from its formatted string. A value such as ₹249.00 is for a reader, not a useful input to arithmetic.
This file-backed catalog is rebuilt from source. Editing the exported array during development is different from allowing a visitor to edit products persistently. A server process variable is not a database and changes to it would not be a reliable multi-user store.
Read this sequence before the source. Identify the input, the component or function that handles it, and the result the learner sees.
The /products page imports the product array.
map creates one ProductCard element per product.
Each card formats its price and builds its details URL from the slug.
The rendered grid displays the three records; the next lesson implements the details destination.
The paths below are relative to your practice project. Each block is the complete file at this checkpoint. Read its responsibility first, type or copy the code, then use the walkthrough to explain the important statements.
This defines the plain product shape and the initial read-only catalog.
export type Product = {
id: string;
slug: string;
name: string;
price: number;
category: string;
description: string;
};
// Prices are integer paise: 24900 means ₹249.00.
export const products: Product[] = [
{
id: "p1",
slug: "learning-notebook",
name: "Learning notebook",
price: 24900,
category: "stationery",
description: "A notebook for diagrams and project notes.",
},
{
id: "p2",
slug: "desk-lamp",
name: "Desk lamp",
price: 89900,
category: "desk",
description: "Light up your practice workspace.",
},
{
id: "p3",
slug: "pen-set",
name: "Pen set",
price: 9900,
category: "stationery",
description: "Three pens for everyday learning.",
},
];
This converts a numeric amount into a readable rupee string at the presentation boundary.
export function money(paise: number) {
return new Intl.NumberFormat("en-IN", {
style: "currency",
currency: "INR",
}).format(paise / 100);
}
This renders one product without owning catalog loading or filtering.
import Link from "next/link";
import type { Product } from "@/lib/products";
import { money } from "@/lib/money";
export default function ProductCard({ product }: { product: Product }) {
return (
<article>
<h2>
<Link href={`/products/${product.slug}`}>{product.name}</Link>
</h2>
<p>{product.description}</p>
<strong>{money(product.price)}</strong>
</article>
);
}
This page owns collection-level loading and renders one reusable card per resulting product.
import { products } from "@/lib/products";
import ProductCard from "@/components/product-card";
export default function ProductsPage() {
return (
<>
<h1>Products</h1>
<div className="grid">
{products.map((product) => (
<ProductCard key={product.id} product={product} />
))}
</div>
</>
);
}
This defines the document and shared structure surrounding the selected page.
import type { ReactNode } from "react";
import Link from "next/link";
import "./globals.css";
export const metadata = {
title: "EduShop",
description: "My Next.js learning project",
};
export default function Layout({ children }: { children: ReactNode }) {
return (
<html lang="en">
<body>
<nav>
<Link href="/">Home</Link>
<Link href="/products">Products</Link>
<Link href="/about">About</Link>
<Link href="/contact">Contact</Link>
</nav>
<main>{children}</main>
</body>
</html>
);
}
Add a fourth object, then reverse the order of the array.
Four cards appear in the new array order without changing ProductCard.
The page derives the UI from data. Stable keys retain the identity of each record even when its position changes.
Use the product ID as key, not the array position. This file is a read-only data source, not a persistent database.
Likely cause: Rupee values were entered where paise are required.
Check and correct: Store 24900 for ₹249.00. Do not remove the division in the formatter to hide inconsistent data.
Likely cause: Two products have the same ID.
Check and correct: Give every product a unique stable ID. Also keep slugs unique so details routes identify one product.
Add a category label to the card and a new product with a unique ID and slug.
Attempt these questions before opening the explanations. Use the source above to justify your answer.
The card needs several fields from the same record. A typed product prop keeps that contract explicit and reusable.
Yes. If the database layer returns the same plain product shape, the page can supply those objects to the existing card.