All week you'll build a Study Board: study cards in three columns, To
do, Doing and Done. It starts as a plain screen today and ends on Day
7 as a tested app with a reducer, context, custom hooks and an error boundary.
The labs are bigger than in the beginner course. Read the folder tree beside
the code first, then open the files in the order the lesson suggests.
Start from the screen, not the code
React's own guide calls this Thinking in React. Before writing any state,
work through five steps:
Break the screen into components. Draw boxes around each piece. A
component should do one job.
Build a static version from data, with props only and no state.
Find the minimal state. Which values change over time, aren't passed
in, and can't be worked out from anything else?
Decide where each piece of state lives: in the closest common parent of
every component that uses it.
Add the reverse data flow: callbacks that let children ask the owner to
change the state.
Here is step 2 for the Study Board. Open App.jsx, Board.jsx, Column.jsx
and CardItem.jsx, in that order.
Runs with Vite on your computer: unzip, then npm install and npm run dev.
The board is drawn entirely from data.js. Nothing changes yet. The search box
isn't connected to anything: type in it and nothing filters.
Step 3: find the minimal state
List every piece of data on the screen, then ask three questions of each:
Data
Changes over time?
Passed in?
Can be computed?
State?
The cards
Yes, later this week
No
No
Yes
The search text
Yes, as you type
No
No
Yes
The cards that match the search
Yes
No
Yes, from the cards and the text
No
Each column's count
Yes
No
Yes, from the matching cards
No
The column names
No
No
No
No, a constant
Only two values are state. Everything else follows from them during render.
Keeping state this small is the most useful habit in this course. The next two
lessons show what goes wrong without it.
Step 4: where does it live?
App shows the search box, and Board shows the matching cards. Their closest
common parent is App, so the search text lives there, and App passes the
matching cards down.
Your turn
Exercise
Connect the search
Make the search box filter the board as you type, ignoring upper and lower
case. Column counts must show the matching cards only.
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
One new piece of state in App, the search text, held by a controlled input.
The matching cards are computed from it, not stored.
Hint 2: a first step
Add the state and a controlled input first, and show query under the box
while you type, to check it's wired up. Then work out the visible cards in
App and pass them to Board instead of starterCards. To ignore case,
change both the title and the search text the same way before comparing. You
won't need to touch Column: it counts whatever cards it's given.