Skip to content

Checkpoint: Study Board v1

The Course
•
20 min study

Close the earlier lessons and build this from the brief. data.js has the columns and the starter cards.

The brief

  • Three columns from columns, each headed with its title and a count, such as To do (2).
  • A search box above the board that filters cards by title, ignoring case. The counts show matching cards only. Next to it, 4 of 4 cards, updated as you type.
  • Clicking a card's title selects it, outlines it, and shows a details panel under the board with its title, column name and minutes.
  • The panel has a Move right button that moves the card to the next column. It's disabled in Done.

Acceptance checks:

  • Select Read about reducers and press Move right twice: it ends in Done, the panel follows it, and Move right is disabled.
  • Search for try: only Try context is left, and the counts read To do (1), Doing (0), Done (0).
  • Your state holds the cards, the search text and the selected id. Nothing else.
Your workspace React · 4 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); // Build the Study Board here: three columns, search, a selected card with a // details panel, and a "Move right" button. data.js has the columns and cards. import { columns, starterCards } from "./data.js"; export default function App() { return ( <main> <h1>Study Board</h1> </main> ); } 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 plan

Do the five steps from the first lesson on paper first. You need App, a Board (or Columns), and a Details panel. Three pieces of state, all in App.

Hint 2: moving right

Find the card's column index in columns, and take the next one: columns[columns.findIndex((c) => c.id === card.column) + 1]. It's undefined past the last column.

Show one solution
One solution React · 6 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { useState } from "react"; import Board from "./Board.jsx"; import Details from "./Details.jsx"; import { columns, starterCards } from "./data.js"; export default function App() { const [cards, setCards] = useState(starterCards); const [query, setQuery] = useState(""); const [selectedId, setSelectedId] = useState(null); // Worked out during render: never stored, so never out of date. const needle = query.trim().toLowerCase(); const visible = cards.filter((card) => card.title.toLowerCase().includes(needle)); const selected = cards.find((card) => card.id === selectedId) ?? null; function moveRight(id) { setCards(cards.map((card) => { if (card.id !== id) return card; const next = columns[columns.findIndex((c) => c.id === card.column) + 1]; return next ? { ...card, column: next.id } : card; })); } 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)} /> <span className="muted">{visible.length} of {cards.length} cards</span> </div> <Board cards={visible} selectedId={selectedId} onSelect={setSelectedId} /> {selected && <Details card={selected} onMoveRight={moveRight} />} </main> ); } import { columns } from "./data.js"; export default function Board({ cards, selectedId, onSelect }) { return ( <div className="board"> {columns.map((column) => { const inColumn = cards.filter((card) => card.column === column.id); return ( <section key={column.id} className="column"> <h2> {column.title} <span className="count">({inColumn.length})</span> </h2> <ul className="cards"> {inColumn.map((card) => ( <li key={card.id} className={card.id === selectedId ? "card selected" : "card"}> <button className="title" onClick={() => onSelect(card.id)}>{card.title}</button> <div className="muted">{card.minutes} min</div> </li> ))} </ul> </section> ); })} </div> ); } 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 }, ]; import { columns } from "./data.js"; export default function Details({ card, onMoveRight }) { const column = columns.find((c) => c.id === card.column); const isLast = card.column === columns.at(-1).id; return ( <section className="panel"> <h2>{card.title}</h2> <p className="muted">{column.title} · {card.minutes} minutes</p> <button onClick={() => onMoveRight(card.id)} disabled={isLast}>Move right</button> </section> ); } 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.

Check yourself

  1. Why store the selected card's id rather than the card?
  2. Name two derived values on this board, and say why neither is state.
  3. When does React keep a component's state, and how do you make it start fresh?

Was this article helpful?

Your response is saved on this device.