Skip to content

Thinking in React

The Course
•
20 min study

This week's project

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:

  1. Break the screen into components. Draw boxes around each piece. A component should do one job.
  2. Build a static version from data, with props only and no state.
  3. Find the minimal state. Which values change over time, aren't passed in, and can't be worked out from anything else?
  4. Decide where each piece of state lives: in the closest common parent of every component that uses it.
  5. 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.

A static board React · 7 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import Board from "./Board.jsx"; import { starterCards } from "./data.js"; export default function App() { return ( <main> <h1>Study Board</h1> <div className="toolbar"> <input aria-label="Search cards" placeholder="Search cards" /> </div> <Board cards={starterCards} /> </main> ); } import Column from "./Column.jsx"; import { columns } from "./data.js"; export default function Board({ cards }) { return ( <div className="board"> {columns.map((column) => ( <Column key={column.id} title={column.title} cards={cards.filter((card) => card.column === column.id)} /> ))} </div> ); } export default function CardItem({ card }) { return ( <li className="card"> <strong>{card.title}</strong> <div className="muted">{card.minutes} min</div> </li> ); } import CardItem from "./CardItem.jsx"; export default function Column({ title, cards }) { return ( <section className="column"> <h2> {title} <span className="count">({cards.length})</span> </h2> <ul className="cards"> {cards.map((card) => ( <CardItem key={card.id} card={card} /> ))} </ul> </section> ); } export const columns = [ { id: "todo", title: "To do" }, { id: "doing", title: "Doing" }, { id: "done", title: "Done" }, ]; export const starterCards = [ { id: 1, title: "Read about reducers", column: "todo", minutes: 20 }, { id: 2, title: "Draw the component tree", column: "doing", minutes: 15 }, { id: 3, title: "Practice useState", column: "done", minutes: 30 }, { id: 4, title: "Try context", column: "todo", minutes: 25 }, ]; body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; }
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:

DataChanges over time?Passed in?Can be computed?State?
The cardsYes, later this weekNoNoYes
The search textYes, as you typeNoNoYes
The cards that match the searchYesNoYes, from the cards and the textNo
Each column's countYesNoYes, from the matching cardsNo
The column namesNoNoNoNo, 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.

Your working space React · 7 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import Board from "./Board.jsx"; import { starterCards } from "./data.js"; export default function App() { return ( <main> <h1>Study Board</h1> <div className="toolbar"> <input aria-label="Search cards" placeholder="Search cards" /> </div> <Board cards={starterCards} /> </main> ); } import Column from "./Column.jsx"; import { columns } from "./data.js"; export default function Board({ cards }) { return ( <div className="board"> {columns.map((column) => ( <Column key={column.id} title={column.title} cards={cards.filter((card) => card.column === column.id)} /> ))} </div> ); } export default function CardItem({ card }) { return ( <li className="card"> <strong>{card.title}</strong> <div className="muted">{card.minutes} min</div> </li> ); } import CardItem from "./CardItem.jsx"; export default function Column({ title, cards }) { return ( <section className="column"> <h2> {title} <span className="count">({cards.length})</span> </h2> <ul className="cards"> {cards.map((card) => ( <CardItem key={card.id} card={card} /> ))} </ul> </section> ); } export const columns = [ { id: "todo", title: "To do" }, { id: "doing", title: "Doing" }, { id: "done", title: "Done" }, ]; export const starterCards = [ { id: 1, title: "Read about reducers", column: "todo", minutes: 20 }, { id: 2, title: "Draw the component tree", column: "doing", minutes: 15 }, { id: 3, title: "Practice useState", column: "done", minutes: 30 }, { id: 4, title: "Try context", column: "todo", minutes: 25 }, ]; body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; }
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.

Show one solution
JSX
import { useState } from "react";
import Board from "./Board.jsx";
import { starterCards } from "./data.js";

export default function App() {
  const [query, setQuery] = useState("");
  const needle = query.trim().toLowerCase();
  const visible = starterCards.filter((card) => card.title.toLowerCase().includes(needle));

  return (
    <main>
      <h1>Study Board</h1>
      <div className="toolbar">
        <input aria-label="Search cards" placeholder="Search cards" value={query}
          onChange={(e) => setQuery(e.target.value)} />
      </div>
      <Board cards={visible} />
    </main>
  );
}

Nothing in Board, Column or CardItem changed. They show whatever cards they're given, which is why building the static version first pays off.

Was this article helpful?

Your response is saved on this device.