Sub topic 1
Start with a mockup and a data model
Every React project that goes well starts before any code. You need two things: a picture of what the user will see, and the data that picture shows. This chapter covers both, using a searchable product table as the running example. The chapters that follow build this table step by step.
Prerequisites
This chapter assumes you already have:
- Basic JavaScript: arrays, objects, arrow functions, and
forEach. - Basic HTML: tables, form inputs, and attributes.
- Some familiarity with JSX and with writing a simple function component.
You do not need to know useState yet. State comes up in later chapters. This chapter has no components in it.
Why start with a mockup
A mockup is a drawing of the finished interface. It can be a sketch on paper, a screenshot, or a file from a design tool. It fixes three things before you write code:
- Which pieces of information appear on screen.
- Which controls the user can operate.
- How the pieces are grouped and laid out.
Without a mockup, you tend to decide these things while coding. The result is components that mirror whatever you typed first rather than the interface you meant to build. Changing a component's boundaries after the fact is slower than changing a drawing.
The mockup for our running example is a small product browser:
- At the top is a search box with a checkbox labeled "Only show products in stock".
- Below it is a table with two columns, Name and Price.
- Rows are grouped under category headings such as "Fruits" and "Vegetables".
- Products that are out of stock show their name in red.
Nothing on this screen is complicated. That is deliberate. The process you learn here matters more than the example.
What the mockup tells you about data
Read the mockup and list every piece of information it shows. For each item, ask where it comes from. Here is the list for the product table:
| What you see | Where it comes from |
|---|---|
| Category headings | The category of each product |
| Product names | The name of each product |
| Prices | The price of each product |
| Red names | Whether the product is in stock |
| Search box text | Typed by the user |
| Checkbox | Toggled by the user |
The first four rows come from data that exists before the user touches anything. The last two come from the user. This split matters a great deal later, when you decide what counts as state. For now, only record it.
The data model
The data for this table arrives as a flat array of objects. In a real app it would come from a server through an API. Here it is hard-coded, which is enough for learning:
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 has four fields:
categoryis a string used to group rows.priceis a string that already includes the currency symbol.stockedis a boolean. It controls the red text and the checkbox filter.nameis a string shown in the table.
Notice what is missing. There is no id field, and there are no category objects. The array is flat, and the grouping you see in the mockup is not stored in the data. The table will have to produce the grouping itself while it renders.
Matching the data to the mockup
The step that people skip is checking that the data actually fits the mockup. Do it explicitly. Walk through each element of the mockup and find the field that feeds it.
Two results come out of this check.
- Every piece of displayed information has a source. If something in the mockup had no source, you would need to change the data or the design now. This is the cheapest moment to find that out.
- The search text and the checkbox value are not in
PRODUCTS. They belong to the user's interaction. Later chapters call this kind of value state.
Things to check in the data
A data model that looks fine can still cause trouble later. Check these points before moving on.
Grouping depends on order
The table you build next shows a category heading whenever the category differs from the previous row's category. This only works if products from the same category sit next to each other in the array. If the array were Apple, Spinach, Dragonfruit, you would get a "Fruits" heading, a "Vegetables" heading, and then a second "Fruits" heading.
You can verify the assumption with a small function:
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' }
];
function isGroupedByCategory(products) {
const seen = new Set();
let last = null;
for (const product of products) {
if (product.category !== last) {
if (seen.has(product.category)) {
return false;
}
seen.add(product.category);
last = product.category;
}
}
return true;
}
console.log(isGroupedByCategory(PRODUCTS)); // true
const shuffled = [PRODUCTS[0], PRODUCTS[3], PRODUCTS[1]];
console.log(isGroupedByCategory(shuffled)); // falseRun this with Node and you will see true and then false. If your real data is not grouped, you have two options: sort it once before rendering, or build a grouped structure. Either is reasonable. This tutorial keeps the flat array and relies on the order, because it is the simplest thing that satisfies the mockup.
Names must be unique to be usable as keys
React needs a stable identifier for each row in a list. In this data set, name is unique, so it can serve. Category names are unique among headings too. That is a property of this sample data and not a general rule. In real data you would use an id from your database. The chapter on rendering lists covers keys in detail.
Prices are strings
'$1' is text, not a number. That works for display, but you could not sort or add these values without parsing them. The mockup only displays prices, so a string is fine. If a later design adds a "sort by price" control, revisit this choice. Storing a number and formatting it at display time is the usual fix.
The data is read-only from the UI's point of view
Nothing in the mockup edits a product. The user can only filter what they see. So the product array never changes while the page is open. This is why PRODUCTS will be passed in as a prop and never copied into state.
Common mistakes at this stage
- Skipping the mockup. You start writing JSX and end up with components shaped around your first guess. Restructuring them costs more than redrawing a sketch.
- Designing the data around the screen. Do not nest the products inside category objects just because the screen shows groups, unless your server already returns them that way. Take the data in the shape you actually get it.
- Storing display decisions in the data. A field like
color: 'red'would duplicate whatstockedalready says. Keep the data about the product, and let the interface decide how to show it. - Forgetting the user-owned values. The search text and checkbox are easy to forget because they are not in the data. List them now so they do not surprise you later.
What you have after this step
At the end of this step you have:
- A mockup that shows the table, the search box, and the checkbox.
- A flat
PRODUCTSarray withcategory,price,stocked, andnameon each item. - A mapping from each visible element to its source, with the search text and checkbox identified as user input.
- A confirmed assumption that products from one category are adjacent in the array.
You have written no components yet. The next step is to draw boxes around the parts of the mockup and name each one. Those boxes become your component hierarchy.
Exercise
You are given a mockup of a music playlist. It shows a title for each song, the artist's name under the title, a duration like "3:45", and a heart icon that is filled in for songs the user has liked. List which of these come from a data model, and sketch one object for a song. Which part would you check about the data before building?
Show answer
The title, artist, duration, and the liked flag come from the data. A song object could be { id: 1, title: 'Blue', artist: 'Ada', duration: '3:45', liked: true }. Before building, check that every visible element has a source field, and that the ids are unique so rows can be keyed. If the heart can be clicked, the liked value changes over time, so it also needs a plan for who owns that change.
Exercise
In the product table, what would go wrong if the array were ordered Apple, Spinach, Dragonfruit, and the table adds a category heading each time the category differs from the previous row?
Show answer
Fruits would appear as a heading twice, once before Apple and again before Dragonfruit, with Vegetables in between. The grouping logic compares each row only to the previous row, so it needs products of the same category to sit together. Sort the array first, or build a grouped structure.