Chapter 15 Β· Putting it together
Thinking in React (Project)
Build a searchable product table from scratch, following the five-step process React developers use for every new feature.
- How to break a design into components
- Why you build a static version first
- How to find the minimal state, and where it should live
- How to make data flow back up from children ("inverse data flow")
What we're building
A product list grouped by category, with a search box and an "Only show products in stock" checkbox:
The data comes from an API as JSON:
Step 1: break the UI into a component hierarchy
Draw boxes around every part of the mockup and give each one a name. A good rule is the single responsibility principle: each component should ideally do one thing.
Step 2: build a static version
First build a version that renders the data but has no interactivity. Use only props, no state yet. Static versions need lots of typing but little thinking; interactivity needs lots of thinking but little typing. Doing them separately makes both easier.
Data flows one way: from the top-level component down through props. This is called one-way data flow.
Step 3: find the minimal but complete state
List every piece of data in the app, then ask three questions about each:
- Does it stay the same over time? Then it's not state.
- Is it passed in from a parent as a prop? Then it's not state.
- Can you calculate it from existing state or props? Then it's definitely not state.
| Data | Is it state? |
|---|---|
| The original list of products | β It's passed in as a prop |
| The search text the user typed | β Yes: it changes and can't be calculated |
| The value of the checkbox | β Yes: it changes and can't be calculated |
| The filtered list of products | β It can be calculated from the products, the search text and the checkbox |
So there are only two pieces of state: filterText and inStockOnly.
Step 4: identify where the state should live
For each piece of state:
- Find every component that uses it.
ProductTableneeds it to filter, andSearchBarneeds it to show the current values. - Find their closest common parent:
FilterableProductTable. - Put the state there.
Then use it in the children:
<input value={filterText} /> is controlled by state, and nothing updates that state yet. Step 5 fixes it.
Step 5: add inverse data flow
The state lives in FilterableProductTable, but the typing happens in SearchBar. Only the owner can change state, so the parent passes its setters down:
The finished app
The five steps, for any feature
- Break the UI into components, one responsibility each.
- Build a static version with props only.
- Find the minimal state: it changes, isn't passed in, and can't be calculated.
- Put the state in the closest common parent of the components that use it.
- Add inverse data flow: pass setters or handlers down so children can request changes.
More examples: the five steps on other apps
Example 1: a contact book
| Data | State? | Lives in |
|---|---|---|
| All contacts | β (can be added or edited) | ContactBook |
| Selected contact ID | β | ContactBook (both the list and the details need it) |
| The selected contact object | β derived: contacts.find(c => c.id === selectedId) | - |
| Number of contacts | β derived: contacts.length | - |
Example 2: a weather widget
The same five questions work for any UI. With practice, you'll do them in your head in seconds.
Project challenges
1. Split into files π’ easy
Move each component into src/components/ with its own file and default export. Import them where they're needed.
Show solution
Each file imports only what it uses. Only FilterableProductTable.jsx needs useState.
2. "No results" message π’ easy
When the filters match nothing, show "No products found" instead of an empty table.
Show hint
In ProductTable, after the loop: if (rows.length === 0) return <p>No products found</p>;
3. Add products π‘ medium
Move PRODUCTS into state in App, and add a small form (name, price, category, in stock) that adds a new product. Use what you learned in Chapter 8.
Show solution
Note: the table groups rows by "last category seen", so new products appear in the right group only if the list is sorted by category. Try products.toSorted((a, b) => a.category.localeCompare(b.category)) before rendering.
4. Remember the filters π‘ medium
Use the useLocalStorage Hook from Chapter 14 so the search text and checkbox survive a page refresh.
Show solution
5. Product count per category π’ easy
Show the number of visible products next to each category heading: "Fruits (2)".
Show solution
Filter first, then count while you group:
6. Clear filters button π’ easy
Add a "Clear" button to SearchBar that resets both filters. Which new prop does it need?
Show solution
Pass an onClear handler from the state owner:
7. Sort by price π‘ medium
Add a dropdown: "Sort: Name / Price (low β high)". Work out the minimal state, where it lives, and how the table uses it. Remember that prices are strings like "$4".
Show solution
a || b sorts by category first, and by name or price only when the categories are equal.
8. Click a row to see details π‘ medium
Clicking a product row shows a details panel under the table ("Apple: $1, in stock"). Clicking the same row again hides it.
Show solution
We store the name (our ID) rather than a copy of the product: the "store IDs, not copies" rule from Chapter 9.
9. Move the state to a reducer π‘ medium
Replace the two useState calls with one useReducer holding { filterText, inStockOnly }, with actions text_changed, stock_toggled and cleared.
Show solution
10. Load products from an API π΄ hard
Replace the hard-coded data with https://dummyjson.com/products?limit=20 (each item has title, price, category, stock). Use the useFetch Hook from Chapter 14, map the data into the shape the table expects, and show "Loadingβ¦" and error states.
Show solution
None of the table components changed. That's the payoff of a good component structure: the data source can change without touching the UI.
π Official docs: Thinking in React