Sub topic 3
Step 1: Break the UI into a component hierarchy
This chapter assumes you can write a function component that returns JSX, pass props to it, and read an array of objects in JavaScript. It does not use state yet. The goal is to turn a mockup into a tree of components before writing any logic.
What this step produces
You start with two inputs from the previous chapter: a mockup of a searchable product table and a JSON-like data model. The output of this step is a list of component names, arranged as a parent and child tree, with a one-line job description for each.
The mockup shows:
- A search box and an "Only show products in stock" checkbox.
- A table with a header row (Name, Price).
- Category heading rows, such as Fruits and Vegetables.
- Product rows. Out-of-stock names appear in red.
The data model is the flat array of products:
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' }
];You decide here where to draw component boundaries. A bad boundary is costly later. If one component does too much, you will later struggle to decide where state belongs.
Ways to find boundaries
Several familiar disciplines suggest where to cut. They often agree, and you can use whichever fits your habits.
- Programming. Use the single responsibility principle. A component should ideally do one thing. If it grows, split it into smaller components.
- CSS. Ask what you would make a class selector for. Each distinct visual block is a candidate.
- Design. Look at how the designer grouped and layered the mockup. If the design file has named layers, those names often match component names.
- Data. Look at the shape of the JSON. UI and data models usually share the same information architecture, so the structure of your data often tells you the structure of your components.
The data approach is useful here. The array is flat, but the table shows it grouped by category. That grouping is a presentation concern. It is not a property of the data itself, and you will need to handle it inside a component.
Applying the rules to the mockup
Work from the outside in.
- Draw a box around the whole mockup. It contains everything: the search controls and the table. Name it
FilterableProductTable. - Draw a box around the search box and checkbox. It is a form that gathers user input. Name it
SearchBar. - Draw a box around the table. Its job is to list products according to the user's filter. Name it
ProductTable. - Inside the table, draw a box around each category heading row. It shows one category name. Name it
ProductCategoryRow. - Draw a box around each product row. It shows one product. Name it
ProductRow.
You could also split the header row (Name, Price) into its own TableHeader component. In this example it is static and tiny, so it stays inside ProductTable. Splitting a piece out that has no logic of its own and is used once only adds names to track. Section "Edge cases" below returns to this decision.
The resulting hierarchy
Nesting in the mockup becomes parent and child in the tree. ProductCategoryRow and ProductRow are children of ProductTable, because they live inside the table.
Read the arrows as "renders". FilterableProductTable renders SearchBar and ProductTable. This tree also describes how data will travel. In React, data moves down the tree through props, so the shape you pick now fixes the paths that data can take later.
A summary table helps you check that each component has one job:
| Component | Single responsibility | Appears |
|---|---|---|
| `FilterableProductTable` | Hold the whole feature together | Once |
| `SearchBar` | Collect the search text and the stock checkbox | Once |
| `ProductTable` | Choose which rows to show and group them by category | Once |
| `ProductCategoryRow` | Show one category heading | Once per category |
| `ProductRow` | Show one product's name and price | Once per product |
If you cannot fill the "single responsibility" cell with one short phrase and no "and", the component probably needs splitting. In the table above, "Collect the search text and the stock checkbox" reads as two things, but both are the same job: gathering the filter input. That is one responsibility.
Mapping components to data
Each component will need some part of the data. Write down which part before you build, because it exposes mistakes early.
ProductCategoryRowneeds one category name.ProductRowneeds one product object.ProductTableneeds the whole products array, so it can group the products and place the category rows.SearchBarneeds two values for the controls. You will define those values in a later step.FilterableProductTablesits above both children. It will need to supply whatever they share.
Notice that ProductTable is the component that turns a flat array into a grouped table. That is why the tree has no Category component that owns a list of products. Your data is flat, so the component that renders it walks the array once and inserts a heading whenever the category changes. A nested design would be reasonable if your data were already nested by category.
A skeleton you can run
Before adding real content, a skeleton confirms that the tree is wired the way the diagram shows. Each component renders a placeholder and nothing else. This code runs in any React project with the function components below, such as a Vite React template.
function SearchBar() {
return <div>SearchBar</div>;
}
function ProductCategoryRow() {
return (
<tr>
<th colSpan="2">ProductCategoryRow</th>
</tr>
);
}
function ProductRow() {
return (
<tr>
<td>ProductRow</td>
<td>$0</td>
</tr>
);
}
function ProductTable() {
return (
<table>
<tbody>
<ProductCategoryRow />
<ProductRow />
</tbody>
</table>
);
}
export default function FilterableProductTable() {
return (
<div>
<SearchBar />
<ProductTable />
</div>
);
}The tr elements sit inside tbody on purpose. HTML tables have a strict structure, and React warns when you put a row directly inside a div. This is a case where the mockup's tags constrain where your component boundaries can sit. ProductRow and ProductCategoryRow must render table rows, so their parent must render a table body.
The skeleton has no props and no data. You add those in the next steps. The point is to check the nesting. If the page shows a search placeholder above a table with two placeholder rows, the tree is correct.
Edge cases and common errors
Splitting too far. If you make a component for every element, you get a long list of files with no behavior. Split when a piece has its own responsibility or will be reused. A reasonable test is whether you could name the component with a noun phrase from the mockup, such as "category row". Names like Wrapper2 mean the split is not helping.
Splitting too little. The opposite error is one large App function that holds the form, the table, and the grouping logic. It works at first. Later, you cannot tell which parts depend on which data, and moving state becomes hard.
Defining components inside other components. Keep each function at the top level of the module. A function declared inside another component's body is a new function on every render. React treats it as a different component each time and throws away its DOM and state. The tree in this chapter is flat on purpose, with each function declared at the top level.
Copying the data shape into the tree. Do not assume one component per field. price and name are not components. They are cells inside ProductRow. Components represent units of UI with a job, not units of data.
Treating the hierarchy as final. The tree is a first draft. Later steps may show that a component needs to move, merge, or split. The last chapter of this tutorial covers iteration. For now, a tree that mirrors the mockup is the right starting point.
Alternatives to this tree
There is more than one reasonable hierarchy. Two common variants:
- A
ProductTableHeadercomponent for the Name and Price row. This helps if the header later gains sorting buttons. It is overhead today. - A
ProductCategorycomponent that renders a heading followed by its products. This helps if the data is already grouped by category. With flat data, you must group it first, which adds work.
Choose the simplest tree that gives every component one job. You can refactor later because the tree only describes how parts relate, and no state is attached to it yet.
Exercise
A designer adds a "Total price" footer row below the product table, showing the sum of the visible products. Where would you place it in the hierarchy, and what single responsibility would its component have?
Show answer
Add a ProductTotalRow component as a child of ProductTable, rendered after the product rows inside the table body (or a tfoot). Its single responsibility is to show one total value in a row. ProductTable already knows which products are visible, so it calculates or receives the total and passes it down as a prop. Placing the row outside ProductTable would break the HTML table structure and leave the footer without access to the visible products.
Exercise
Why is there no Category component that contains a list of ProductRow children in this chapter's hierarchy?
Show answer
The data is a flat array, and grouping is only a presentation detail. ProductTable walks the array once and inserts a ProductCategoryRow whenever the category changes. A nested Category component would require grouping the data first. It would also sit awkwardly inside a table, because rows must stay directly inside the table body.