Introduction
You now know every core piece of React: components, JSX, props, lists, conditions, events, state, effects, refs, reducers and context. Knowing the pieces is not the same as knowing where to start on a real screen. This lesson follows the process React's own documentation recommends for turning a design into components, step by step, using a searchable book table as the example.
How it works
Step 1: break the UI into a component hierarchy. Draw boxes around every part of the design and give each a name. A component should do one thing; if a box does several, split it. For the book table: App (the whole thing) contains a SearchBar and a BookTable, which shows a genre heading row and a row per book. The boxes nest, and that nesting is the component tree.
Step 2: build a static version. Write the components so they render the data through props, with no state and no interactivity at all. This is mostly typing, and it gives you a working layout to build on. Data flows down: App passes the books to BookTable.
Step 3: find the minimal state. List every piece of data in the app and ask three questions of each. Is it passed in from a parent? Then it is a prop. Does it stay the same over time? Then it is not state. Can it be computed from other state or props? Then it is not state. What is left is the minimal state. For the table: the book list is constant data; the search text and the "only available" checkbox change and cannot be computed, so they are state; the filtered list can be computed from them, so it is not.
Step 4: decide where each piece of state lives. Find every component that uses it, and put it in their closest common parent. SearchBar shows the search text and BookTable needs it to filter, so the state belongs in App.
Step 5: add inverse data flow. The inputs live in SearchBar, but the state lives in App. Pass the setters down as callback props (onText, onOnlyAvailable), and have the inputs call them in onChange. Data now flows down as props and changes flow up as callbacks.
Following these steps keeps state small and in the right place, which is what keeps a growing app understandable.
Example
Line by line
BOOKSis constant data, so it is not state.SearchBaris fully controlled: it shows the values it is given and reports changes through callbacks.BookTableonly renders what it receives, grouping the books by genre.Appowns the two pieces of state and computes the filtered list during render.
Expected result
The preview shows a search box, an "Only available" checkbox and a table with Science and Fiction headings over four books. Ticking the checkbox hides "The Selfish Gene"; typing "pa" then leaves only Pather Panchali and Things Fall Apart, under Fiction.
In the real world
Front-end teams plan screens exactly this way, often in a design review: which components exist, which are shared with other screens, what state each screen needs and where it lives. Getting the state structure right early avoids most of the bugs that come from duplicated or misplaced state later.
Common mistakes
- Starting with state and effects before a static version renders correctly.
- Storing derived data, such as the filtered list, in state as well.
- Putting state in the component that shows it rather than in the parent that all its users share.
- Making one huge component instead of splitting the design into pieces with one job each.
Best practices
- Build top-down for small apps and bottom-up for large ones, but always static first.
- Keep state minimal, and compute everything else during render.
- Name callback props after what happened, and keep presentational components free of state.
Summary
To build a React interface, break the design into a component hierarchy, build a static version with props, identify the minimal state, place each piece of state in the closest common parent of its users, and pass changes back up through callback props. Data flows down, changes flow up, and everything that can be computed is computed.
Quick check
Key takeaways
- Draw the component hierarchy, then build it with props only.
- State is what changes over time, is not passed in and cannot be computed; keep it minimal.
- Put each piece of state in the closest common parent of its users, and pass setters down as callbacks.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.