Skip to content

Checkpoint: Autosave Notes

The Course
•
30 min study

Close the earlier lessons and work from the brief. This is a new problem, not a copy of a lab: decide for each effect whether it's needed at all, and what should restart it.

The brief

The starter is a notes box that autosaves every two seconds through a pretend service, src/autosave.js. It looks as if it works, but watch the console while you type: every keystroke logs autosave stopped and autosave started. Someone who types steadily, a key at least every two seconds, is never saved at all. Ticking Quiet saves restarts it too.

Change App.jsx so that:

  • The word count is worked out during render. It needs no state and no effect.
  • The autosave starts once, when the page appears, and nothing you type or tick restarts it.
  • Each save still saves the latest note, and updates the status line to Last saved: and the note, unless Quiet saves is ticked. Quiet saves still save; they just don't change the status line.

Acceptance checks:

  • Typing hello world shows Words: 2 straight away.
  • The console shows autosave started once, at the start. Typing and ticking Quiet saves log no autosave stopped.
  • Two seconds after you stop typing hello world, the console shows saved: hello world and the status reads Last saved: hello world.
  • With Quiet saves ticked, type more: the next save logs the new text, and the status line stays as it was.
  • App.jsx has exactly one useEffect, and its dependency list is empty.
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 />); import { useEffect, useState } from "react"; import { saveNote, startAutosave } from "./autosave.js"; export default function App() { const [note, setNote] = useState(""); const [quiet, setQuiet] = useState(false); const [words, setWords] = useState(0); const [status, setStatus] = useState("Not saved yet"); useEffect(() => { setWords(note.trim() === "" ? 0 : note.trim().split(/\s+/).length); }, [note]); useEffect(() => { return startAutosave(() => { saveNote(note); if (!quiet) setStatus(`Last saved: ${note}`); }); }, [note, quiet]); return ( <main> <h1>Study notes</h1> <textarea aria-label="Notes" rows={4} value={note} onChange={(e) => setNote(e.target.value)} /> <p>Words: {words}</p> <label> <input type="checkbox" checked={quiet} onChange={(e) => setQuiet(e.target.checked)} /> Quiet saves </label> <p className="muted">{status}</p> </main> ); } // A pretend autosave service. A real one would send the note to a server; // this one calls onTick every two seconds and logs what it saves. export function startAutosave(onTick) { console.log("autosave started"); const id = setInterval(onTick, 2000); return () => { console.log("autosave stopped"); clearInterval(id); }; } export function saveNote(text) { console.log(`saved: ${text}`); } 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: which effect, which tool?

Ask of each effect: does it keep something outside React in step? The word count doesn't: it's calculated from note, which you already have. The autosave does, so it stays an effect, but note and quiet are only needed at the moment a save happens, not to decide when to start the service. That's the job useEffectEvent did for the chat's Muted box.

Hint 2: a first step

Remove the word-count state and its effect first, and check Words still updates. Then move the body of the autosave callback into a function made with useEffectEvent, and have the effect's callback call it. What's left in the effect's dependency list?

Show one solution
One solution React · 4 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { useEffect, useEffectEvent, useState } from "react"; import { saveNote, startAutosave } from "./autosave.js"; export default function App() { const [note, setNote] = useState(""); const [quiet, setQuiet] = useState(false); const [status, setStatus] = useState("Not saved yet"); // Worked out from note on every render: no state, no effect. const words = note.trim() === "" ? 0 : note.trim().split(/\s+/).length; // Reads the latest note and quiet, but isn't a dependency. const onTick = useEffectEvent(() => { saveNote(note); if (!quiet) setStatus(`Last saved: ${note}`); }); useEffect(() => { return startAutosave(() => onTick()); }, []); return ( <main> <h1>Study notes</h1> <textarea aria-label="Notes" rows={4} value={note} onChange={(e) => setNote(e.target.value)} /> <p>Words: {words}</p> <label> <input type="checkbox" checked={quiet} onChange={(e) => setQuiet(e.target.checked)} /> Quiet saves </label> <p className="muted">{status}</p> </main> ); } // A pretend autosave service. A real one would send the note to a server; // this one calls onTick every two seconds and logs what it saves. export function startAutosave(onTick) { console.log("autosave started"); const id = setInterval(onTick, 2000); return () => { console.log("autosave stopped"); clearInterval(id); }; } export function saveNote(text) { console.log(`saved: ${text}`); } 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. Two components call the same custom hook. Do they share its state?
  2. Name three kinds of code that look like they need an effect but don't.
  3. What problem does useEffectEvent solve?

Was this article helpful?

Your response is saved on this device.