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.
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.
After saving to history, are the history snapshot and the current list truly independent?
After saving to history, are the history snapshot and the current list truly independent?
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.