Sub topic 1
Start with a mockup and a data model
Prerequisites
Assume you know basic JavaScript arrays, objects, and arrow functions. You should also recognize HTML elements and basic JSX syntax.
Start with a mockup and a data model
Begin with the visible parts of the design. A searchable product table needs a search form, a table header, category headings, and product rows. Separately, define the data that the interface displays:
const PRODUCTS = [
{ category: 'Fruits', price: '$1', stocked: true, name: 'Apple' },
{ category: 'Fruits', price: '$1', stocked: true, name: 'Dragonfruit' },
{ category: 'Vegetables', price: '$2', stocked: true, name: 'Spinach' }
];The array is the data model. It describes products without describing their visual layout.
Quick review: components, JSX, and props
A React component is a function that returns user-interface elements. JSX is the HTML-like syntax used inside JavaScript. Props are inputs that a parent component passes to a child component.
function ProductRow({ product }) {
return (
<tr>
<td>{product.name}</td>
<td>{product.price}</td>
</tr>
);
}ProductRow receives one product through props and renders one table row.
Step 1: Break the UI into a component hierarchy
Use the single responsibility principle: each component should have one clear job. A useful hierarchy is:
FilterableProductTable
├── SearchBar
└── ProductTable
├── ProductCategoryRow
└── ProductRowThis structure lets each part change independently. The parent eventually coordinates filtering, while the table focuses on displaying rows.
Rendering rows from an array, and why keys matter
Use map to turn each product into a component. A key is a stable identifier that helps React match each rendered item with its data when the array changes.
function ProductTable({ products }) {
return (
<table>
<tbody>
{products.map((product) => (
<ProductRow key={product.name} product={product} />
))}
</tbody>
</table>
);
}Choose a key from the data, such as a database ID. Avoid array indexes when rows can be inserted, removed, or reordered.
Step 2: Build a static version in React
A static version has no state. It receives products as props and renders the same output for the same input. Add category rows while iterating through the ordered array, then pass each product to ProductRow.
Exercise
Why should the filtered product list usually not be stored as state?
Show answer
It can be derived from the products and filter values during rendering, so storing it creates duplicate data that can become inconsistent.