Sub topic 1
Start with a mockup and a data model
Prerequisites
This chapter assumes you know the following:
- Basic JavaScript, including arrays, objects, and arrow functions.
- Basic HTML elements and attributes.
- How to read JSX and write a simple function component.
You do not need to know React state yet. This chapter only sets up the starting material for the rest of the tutorial.
Why start here
Thinking in React is a process for building a UI in a fixed order. You do not begin by writing components. You begin with two inputs: a design mockup and a data model. Every later step, from splitting the UI into components to deciding where state lives, comes from these two inputs.
The running example is a searchable product table. A user sees a list of products grouped by category. A search box filters the list by name. A checkbox hides products that are out of stock.
The mockup
A mockup is a picture of the finished UI. It might come from a designer or from your own sketch. For this example, picture the following:
- A search box at the top, with the placeholder text "Search...".
- A checkbox below it, labeled "Only show products in stock".
- A table with the columns Name and Price.
- Category heading rows ("Fruits", "Vegetables") that group the products.
- Products that are out of stock, shown with red names.
Read the mockup for two kinds of information. The first is structure: which boxes appear and how they nest. You will use that in the next chapter to draw component boundaries. The second is data: what values the screen displays. That is the subject of the next section.
The data model
A data model is the information your UI shows, expressed as plain JavaScript values. In a real app it usually comes from an API. Here it is a constant array.
const PRODUCTS = [
{ category: 'Fruits', price: '$1', stocked: true, name: 'Apple' },
{ category: 'Fruits', price: '$1', stocked: true, name: 'Dragonfruit' },
{ category: 'Fruits', price: '$2', stocked: false, name: 'Passionfruit' },
{ category: 'Vegetables', price: '$2', stocked: true, name: 'Spinach' },
{ category: 'Vegetables', price: '$4', stocked: false, name: 'Pumpkin' },
{ category: 'Vegetables', price: '$1', stocked: true, name: 'Peas' }
];Each product is an object with four fields:
| Field | Type | Used for |
|---|---|---|
| `category` | string | Grouping rows under a heading |
| `price` | string | The Price column |
| `stocked` | boolean | Red name, and the in-stock filter |
| `name` | string | The Name column, and the search filter |
The array is flat. Products are not nested inside categories. Each product records its own category, and the table groups them while rendering. This works because the products are already ordered by category. If your data arrives in a different order, you would need to sort it first.
Matching the mockup to the data
Check that every piece of the mockup can be traced to your data. Where it can't, you have found a gap early, before writing any code.
The last branch matters. The search text and the checkbox value appear in the UI, but they are not in PRODUCTS. They come from the user and change over time. Later chapters will treat them as state. For now, note that the data model describes what exists, and the user's input describes how they want to view it.
Common mistakes at this stage
- Starting with components. If you split the UI before you know the data, the boundaries often follow the visual layout and not the data shape. They then need rework.
- Nesting the data to match the visual grouping. Keep the data in the shape your source provides, and derive groupings when rendering.
- Adding fields "just in case". Each field should be shown or used by a filter.
Exercise
The mockup shows red names for out-of-stock products. Which field of a product supports this, and does the search text need to be in the PRODUCTS array?
Show answer
The stocked boolean decides whether the name is red. The search text does not belong in PRODUCTS. It comes from the user and changes over time, so it is handled separately as state in a later step.