Skip to content

Come Back Later

The Course
•
20 min study

Come back to this a few days after finishing the course. Remembering something after a gap is what makes it stick. Try everything here without looking back at earlier lessons first.

Bug hunt

This small app has four bugs. It doesn't even start: the preview is blank and the console says Too many re-renders. Find the bug that causes that first. Once the app runs, watch the page for a few seconds, click Add a note, and read the console. Then find the other three.

Four bugs React · 3 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { useEffect, useState } from "react"; export default function App() { const [notes, setNotes] = useState(["Props flow down", "State is a snapshot"]); const [seconds, setSeconds] = useState(0); useEffect(() => { setInterval(() => { setSeconds(seconds + 1); }, 1000); }, []); function addNote() { notes.push(`Note ${notes.length + 1}`); setNotes(notes); } return ( <main> <h1>Review notes</h1> <p className="muted">Open for {seconds} seconds</p> <ul> {notes.map((note) => ( <li>{note}</li> ))} </ul> <button onClick={addNote()}>Add a note</button> </main> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 1rem; margin: 1rem 0; } label { display: block; margin: 0.5rem 0; } input, button { font: inherit; } .error { color: #b42318; } .muted { color: #6b7785; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: what to look for
  • Too many re-renders means something sets state while the component is rendering, so every render asks for another one. Look for a function that is called in the JSX instead of passed.
  • Once it runs: does the seconds counter keep going?
  • Does clicking Add a note show a new note?
  • What does React warn about in the console?
Hint 2: the days

Day 3 (events and state), Day 4 (keys and arrays) and Day 6 (effects and cleanup) each have at least one.

Show the four bugs and the fixed app
  1. onClick={addNote()} calls addNote during every render, instead of passing it for React to call on a click. addNote sets state, which asks for another render, which calls addNote again, so React stops with Too many re-renders. Fix: onClick={addNote}. (Day 3)
  2. notes.push(...) then setNotes(notes) changes the array in place and hands React the same array, so clicking shows nothing. Fix: setNotes([...notes, newNote]). (Day 4)
  3. No key on the <li> elements, so React warns. Fix: a stable, unique key. (Day 4)
  4. The interval reads a stale seconds and has no cleanup. setSeconds(seconds + 1) always uses the first render's 0, so the counter sticks at 1. Fix: an updater function, and return a cleanup that clears the interval. (Day 6)
Fixed React · 3 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { useEffect, useState } from "react"; export default function App() { const [notes, setNotes] = useState(["Props flow down", "State is a snapshot"]); const [seconds, setSeconds] = useState(0); useEffect(() => { const id = setInterval(() => { setSeconds((s) => s + 1); }, 1000); return () => clearInterval(id); }, []); function addNote() { setNotes([...notes, `Note ${notes.length + 1}`]); } return ( <main> <h1>Review notes</h1> <p className="muted">Open for {seconds} seconds</p> <ul> {notes.map((note) => ( <li key={note}>{note}</li> ))} </ul> <button onClick={addNote}>Add a note</button> </main> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 1rem; margin: 1rem 0; } label { display: block; margin: 0.5rem 0; } input, button { font: inherit; } .error { color: #b42318; } .muted { color: #6b7785; }
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 against the lesson it came from.

  1. What does root.render(<App />) do? (Day 1)
  2. Why does minutes="40" cause trouble when you add to it? (Day 2)
  3. Why does {count && <p>…</p>} sometimes show a 0? (Day 2)
  4. Three calls to setCount(count + 1) in one click add only 1. Why? (Day 3)
  5. Why is key={index} a problem in a list where items can be removed? (Day 4)
  6. What are the two props of a controlled input? (Day 5)
  7. Where should state go when two siblings need it? (Day 5)
  8. When does React call an effect's cleanup function? (Day 6)
  9. Why do Study Tasks' tests clear localStorage before each test? (Day 7)

Ready for the next step?

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

  • build a component tree from a brief, with props and callbacks
  • choose where each piece of state lives
  • update arrays and objects in state without changing them in place
  • load data in an effect, with loading and error states and a cleanup
  • run a React project with Vite and test it with Vitest

If any of these feels shaky, redo that day's checkpoint from scratch. It's quicker than you'd expect the second time.

Was this article helpful?

Your response is saved on this device.