This editor lets you pick a card and change its title. It has a bug that surprises almost everyone the first time.
npm install and npm run dev. Predict
The editor shows Read about reducers. Click Practice useState in the toolbar. What will the title field contain?
Check your prediction
It still says Read about reducers, although the heading changed to Editing card 3. Press Save now, and card 3 is renamed to the wrong title.
State belongs to a position in the tree
useState(card.title) uses its argument only on the first render. After
that, React keeps the state for as long as the component stays in the same
position in the tree. Clicking another card re-renders CardEditor in the
same place, with a different card prop, so React keeps the old draft:
export default function CardEditor({ card, onSave }) {
const [draft, setDraft] = useState(card.title);
return (
<section className="panel">
<h2>Editing card {card.id}</h2>
<input aria-label="Title" value={draft} onChange={(e) => setDraft(e.target.value)} />{" "}
<button onClick={() => onSave(card.id, draft)}>Save</button>
</section>
);
}That's usually what you want. Typing in a field shouldn't be lost because a parent re-rendered. Here, though, a different card should start a fresh draft.
A key resets state
You used keys on Day 4 of the beginner course to tell list items apart. They work on any component. A different key tells React it's a different component, so it throws the old one away, state included, and makes a new one.
In App.jsx, give the editor the card's id as its key:
<CardEditor key={selected.id} card={selected} onSave={save} />Now each card gets its own fresh editor, and its draft starts from that card's title. Repeat the experiment.
Two more rules about position
- Same position, same type: state is kept. Even if you write the component
in a different branch of a ternary,
{isEditing ? <Editor /> : <Editor />}keeps one state. - A different type at that position: state is reset. Swapping
<Editor />for<Preview />destroys the editor's state, even if you swap back later.
Avoid the alternative
Without a key, you might sync the draft with an effect:
useEffect(() => {
setDraft(card.title);
}, [card.id]);It renders once with the stale draft, then again with the right one, and it spreads the "reset when the card changes" rule across two places. The key says it in one word.
Keep unsaved drafts separate
With the key in place, start editing card 1, switch to card 2, then back to card 1. Your unsaved change to card 1 is gone. Explain why, then say how you'd keep each card's unsaved draft, without changing what you've learned about keys.
Hint 1: the idea
Keys distinguish component identities; they do not archive state for components that have unmounted.
Hint 2: a first step
The key throws the old editor away, state included. If drafts must survive, where could they live instead?
Show one solution
Switching cards gives the editor a new key, so React destroys it, along with
its draft. Coming back makes a brand-new editor whose draft starts from the
saved title.
To keep unsaved drafts, lift them out of the editor: store them in App as an
object keyed by card id, such as { 1: "Read about reduc", 3: "Practice" }.
The editor then receives its draft and an onDraftChange callback as props.
State that has to outlive a component lives higher up.
Completion is saved in this browser only. It does not measure mastery.