Thinking in React
From design to components.
Given a design, how do you turn it into React? There's a simple process. Break the page into components, build a static version, then add just the state you need.
- Start from a design
- Break it up
- Add state last
Five steps
First, draw boxes around each part of the design, and name them. Second, build a static version with props only. Third, find the minimal state. Fourth, decide where that state lives. Fifth, pass functions down, so children can change it.
- Draw boxes
- Static version
- Minimal state
- Where it lives
- Functions down
A shop page
Our shop page becomes a tree of components. The app holds a search bar and a product table. The table holds a row for each product.
Minimal state
Ask three questions about each piece of data. Does it change over time? Is it passed in from a parent? Can you calculate it from other data? Only data that changes, and can't be passed or calculated, is state.
| key | value |
|---|---|
| Changes? | maybe state |
| Passed in? | a prop |
| Calculated? | not state |
| Else | state |
Summary
Break the design into components, build it static, then add the minimal state. Next, where state lives.
Key takeaways- Boxes
- Static first
- Minimal state