Skip to content

Come Back Later

The Course
•
20 min study

Come back to this a few days after finishing the course. Try everything without looking back at earlier lessons first.

Bug hunt

This board has four bugs, from Days 1, 2 and 5. Try these, in order, and note what goes wrong each time:

  1. Clear the console, then type try quickly in the search box.
  2. Rename Read about reducers to Reducers! with the editor.
  3. Select Try context in the list.
  4. Remove a card with ×.
Four bugs React · 5 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); // A pretend server: logs each request, like the Day 5 search. export async function searchCards(cards, query) { console.log(`request: "${query}"`); await new Promise((resolve) => setTimeout(resolve, 200)); return cards.filter((card) => card.title.toLowerCase().includes(query.toLowerCase())); } import { useEffect, useReducer, useState } from "react"; import { searchCards } from "./api.js"; import { starterCards } from "./data.js"; function boardReducer(cards, action) { switch (action.type) { case "card_renamed": return cards.map((card) => (card.id === action.id ? { ...card, title: action.title } : card)); case "card_removed": cards.filter((card) => card.id !== action.id); default: throw new Error(`Unknown action: ${action.type}`); } } function useDebouncedValue(value, delay) { const [debounced, setDebounced] = useState(value); useEffect(() => { setTimeout(() => setDebounced(value), delay); }, [value, delay]); return debounced; } function Editor({ card, dispatch }) { const [draft, setDraft] = useState(card.title); return ( <section className="panel"> <h2>Editing: {card.title}</h2> <input aria-label="Rename" value={draft} onChange={(e) => setDraft(e.target.value)} />{" "} <button onClick={() => dispatch({ type: "card_renamed", id: card.id, title: draft })}>Rename</button> </section> ); } export default function App() { const [cards, dispatch] = useReducer(boardReducer, starterCards); const [selected, setSelected] = useState(starterCards[0]); const [query, setQuery] = useState(""); const [results, setResults] = useState(starterCards); const debouncedQuery = useDebouncedValue(query, 300); useEffect(() => { let ignore = false; searchCards(cards, debouncedQuery).then((found) => { if (!ignore) setResults(found); }); return () => { ignore = true; }; }, [cards, debouncedQuery]); 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> <ul className="cards"> {results.map((card) => ( <li key={card.id} className={card.id === selected.id ? "card selected" : "card"}> <button className="title" onClick={() => setSelected(card)}>{card.title}</button>{" "} <button onClick={() => dispatch({ type: "card_removed", id: card.id })} aria-label={`Remove ${card.title}`}>×</button> </li> ))} </ul> <Editor card={selected} dispatch={dispatch} /> </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: what you should have seen
  1. Three request lines, one per letter.
  2. The list says Reducers!, but the editor's heading still says Editing: Read about reducers.
  3. The heading changes to Try context, but the box still says Reducers!.
  4. The whole page disappears: Unknown action: card_removed.
Hint 2: where to look

The debounce hook's effect; the selected state; how Editor is rendered; and the reducer's card_removed case.

Show the four bugs and the fixed app
  1. The debounce never cancels (Day 5). Each keystroke starts a timer, and nothing clears the old ones. Return a cleanup: const id = setTimeout(...); return () => clearTimeout(id);.
  2. The selection is a copied card (Day 1). The reducer makes a new card object when renaming; the copy in selected keeps the old title. Store selectedId and look the card up during render.
  3. The editor keeps its draft (Day 1). It stays in the same place, so its state survives. Give it key={selected.id}.
  4. A case without return (Day 2). card_removed falls through to default and throws. Return the filtered cards. An error boundary would also have kept the rest of the page on screen.
Fixed React · 5 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); // A pretend server: logs each request, like the Day 5 search. export async function searchCards(cards, query) { console.log(`request: "${query}"`); await new Promise((resolve) => setTimeout(resolve, 200)); return cards.filter((card) => card.title.toLowerCase().includes(query.toLowerCase())); } import { useEffect, useReducer, useState } from "react"; import { searchCards } from "./api.js"; import { starterCards } from "./data.js"; function boardReducer(cards, action) { switch (action.type) { case "card_renamed": return cards.map((card) => (card.id === action.id ? { ...card, title: action.title } : card)); case "card_removed": return cards.filter((card) => card.id !== action.id); default: throw new Error(`Unknown action: ${action.type}`); } } function useDebouncedValue(value, delay) { const [debounced, setDebounced] = useState(value); useEffect(() => { const id = setTimeout(() => setDebounced(value), delay); return () => clearTimeout(id); }, [value, delay]); return debounced; } function Editor({ card, dispatch }) { const [draft, setDraft] = useState(card.title); return ( <section className="panel"> <h2>Editing: {card.title}</h2> <input aria-label="Rename" value={draft} onChange={(e) => setDraft(e.target.value)} />{" "} <button onClick={() => dispatch({ type: "card_renamed", id: card.id, title: draft })}>Rename</button> </section> ); } export default function App() { const [cards, dispatch] = useReducer(boardReducer, starterCards); const [selectedId, setSelectedId] = useState(starterCards[0].id); const selected = cards.find((card) => card.id === selectedId) ?? cards[0]; const [query, setQuery] = useState(""); const [results, setResults] = useState(starterCards); const debouncedQuery = useDebouncedValue(query, 300); useEffect(() => { let ignore = false; searchCards(cards, debouncedQuery).then((found) => { if (!ignore) setResults(found); }); return () => { ignore = true; }; }, [cards, debouncedQuery]); 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> <ul className="cards"> {results.map((card) => ( <li key={card.id} className={card.id === selected?.id ? "card selected" : "card"}> <button className="title" onClick={() => setSelectedId(card.id)}>{card.title}</button>{" "} <button onClick={() => dispatch({ type: "card_removed", id: card.id })} aria-label={`Remove ${card.title}`}>×</button> </li> ))} </ul> {selected && <Editor key={selected.id} card={selected} dispatch={dispatch} />} </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.

Recall questions

Answer each in a sentence or two, then check the lesson it came from.

  1. What are the three questions that decide whether a value is state? (Day 1)
  2. How do you make a component's state start fresh when a prop changes? (Day 1)
  3. Why must a reducer be pure, and where does Date.now() go instead? (Day 2)
  4. Why put state and dispatch in separate contexts? (Day 3)
  5. When would you choose composition over context? (Day 3)
  6. Changing a ref doesn't re-render. When is that what you want? (Day 4)
  7. Name three things that look like they need an effect, but don't. (Day 5)
  8. Why does memo need stable props, and how do you keep a function stable? (Day 6)
  9. Which errors can't an error boundary catch? (Day 7)

Ready for the next step?

You're ready to move on if you can, without help:

  • choose minimal state, and say where each piece lives
  • move a component's updates into a tested reducer
  • share state through context with checked hooks
  • extract logic into a custom hook, and tell when an effect isn't needed
  • keep a slow screen responsive, and handle a form with actions
  • test a reducer, a component and a hook with Vitest

If one of these feels shaky, redo that day's checkpoint from scratch.

Was this article helpful?

Your response is saved on this device.