Training Grounds
Undo Brings Back the Wrong NameBlue Belt
+25 Ki
Description

A name editor lets the user rename items in a list and undo the last change. Renaming works correctly — the new name appears immediately. Clicking Undo should restore the previous name but instead the name stays as the edited version.

Anomaly

Click Edit on any item, type a new name, click Save — the name updates correctly. Click Undo — the name does not revert. The original name is gone.

Constraint
Do not remove the undo feature or the history state
Hint

After saving to history, are the history snapshot and the current list truly independent?

Consult the SenseiOnly for those truly stuck · Flip to reveal

After saving to history, are the history snapshot and the current list truly independent?

Loading editor…
Correct Solution
Loading...

Why this fixes it

`handleSave` pushed `items` into history and then mutated a nested object inside `[...items]` — the spread created a new array but the objects inside it were still the same references, so the history snapshot and the updated list shared the same item objects, making undo restore the already-mutated data. Replacing the mutation with `setItems(prev => prev.map(i => i.id === id ? { ...i, name: draft } : i))` creates a genuinely new object for the edited item, leaving every object already stored in history completely untouched. When undo restores a history snapshot, it now holds the original, pre-edit references and the previous name reappears correctly.

Expected OutputGoal State
Your OutputLive