Sub topic 3
Step 1: Break the UI into a component hierarchy
This chapter assumes you can write a basic function component with JSX and know what props are. It also assumes you have the mockup of the searchable product table and its JSON data from the previous chapters. The goal here is to turn that mockup into a tree of components before writing any real code.
Draw boxes around the mockup
Start by drawing a box around every visual piece of the mockup. Then give each box a name. Do this on paper, in a design tool, or in the browser inspector. If you work with a designer, they may have already named the layers, and those names are often good component names.
For the product table, the boxes look like this:
- The whole thing: a filterable table that holds everything.
- The search area: a text input and a checkbox.
- The table: the header and all the rows.
- A category heading row, such as "Fruits".
- A product row, such as "Apple, $1".
Decide what deserves its own component
Use the single responsibility principle to decide where to split. A component should do one job. When a component grows to do several jobs, split it.
Other disciplines give you matching heuristics:
- Programming: a function or class is a good size when it does one thing. A component is the same idea.
- CSS: if you would make a class selector for it, it may be a component.
- Design: if the layers of the mockup form natural groups, each group is a candidate.
Your data helps too. JSON data models usually map closely to the UI. A product has a category, price, stock flag, and name, and a row shows those fields. That match often tells you where the component boundaries go.
The resulting hierarchy
Five components come out of the mockup. Indentation shows nesting.
The table has no separate component for the header row. That is a valid choice. The header is only two cells, and it has no behavior of its own. If it later gains sorting, it becomes a good candidate for extraction. The chapter on iterating on the design returns to this trade-off.
Turn the tree into a skeleton
The hierarchy is a parent-child relationship. FilterableProductTable renders SearchBar and ProductTable. ProductTable renders the two row components. Parents pass data down to children through props.
You can capture the tree as empty components. The skeleton below renders nothing real yet, but it fixes the names and the nesting.
function SearchBar() {
return <form>{/* inputs go here */}</form>;
}
function ProductCategoryRow() {
return null;
}
function ProductRow() {
return null;
}
function ProductTable() {
return (
<table>
<tbody>{/* rows go here */}</tbody>
</table>
);
}
function FilterableProductTable() {
return (
<div>
<SearchBar />
<ProductTable />
</div>
);
}Each function name starts with a capital letter. React treats lowercase tags as HTML elements, so <searchBar /> would be looked up as an unknown DOM element.
Common mistakes
- Splitting too fine. A component for every
<td>adds files and props without adding clarity. Split where there is a distinct job. - Splitting too coarse. One 200-line component that renders the search form, the table, and the rows is hard to test and reuse.
- Naming by appearance.
RedTextdescribes style.ProductRowdescribes purpose. Purpose names survive redesigns. - Flat structure. If the hierarchy has everything at one level, you probably have not found the nesting that the mockup already shows. Rows belong inside a table.
- Nesting component definitions. Define each component at the top level of the file, not inside another component's body. A nested definition creates a new component type on every render, and React then resets its state.
Exercise
A mockup of a chat app shows a list of messages. Each message has an avatar, an author name, and a text body. Below the list is a box to type a new message with a Send button. Propose a component hierarchy of at least four components, and name the parent of each.
Show answer
One valid answer: ChatApp (top level) renders MessageList and MessageComposer. MessageList renders many Message components. Message may render Avatar, though Avatar can stay inside Message if it has no behavior of its own. The names describe purpose, nesting follows the layout, and the list and the composer have separate jobs.