The Cancel Button That Didn't: Why Modal Edits Leak Into Your List
When a cancel button can't be trusted to discard edits, users lose confidence and data gets silently corrupted. This pattern—mutating state through a shared reference—is a classic React footgun that shows up in any list-with-editor UI, and the fix is a reusable discipline for keeping form state isolated.
When an edit modal in React fills its form with `setForm(row)`, it stores a reference to the original row object, not a copy. Any mutation inside the form—like `form.name = name`—silently changes the list row itself. The later `setForm({ ...form })` only creates a new object after the original has already been mutated, so Cancel just closes the window; it can't undo what was changed. The bug sometimes appears delayed because React only re-renders the list when the state actually updates, leaving the UI showing stale values until the next render.
Shallow copying the row on open solves simple cases but breaks when the row has nested objects. Spreading the outer object still shares references to inner objects like `address`, so editing `form.address.city` leaks the change into the list. The article's practical fixes include building the form from only the needed fields (so the form never touches nested list objects), copying every layer of nested structure when updating, or using `structuredClone` when the data supports it—though functions and class prototypes don't clone cleanly.
The real fix is architectural: treat the form as fully independent state and only update the list after a successful save. The article specifically advises against patching the Cancel button to restore old values, since that requires tracking every field and reintroduces the bug whenever a field is missed. Save should replace the row from the server response, and the UI should block Cancel and inputs during an in-flight request, otherwise a user can click Cancel while the server still saves the data.
The delayed symptom—data changes only appearing on the next render—is a direct consequence of bypassing React's state update cycle, which is why the bug is so easy to miss during quick manual testing.
Adding a restore-on-cancel mechanism is a band-aid that scales poorly as fields accumulate; the durable solution is to keep the list immutable during editing and treat Save as the single commit point.
The article's advice to disable the UI during an in-flight save is the only way to make Cancel unambiguous—otherwise the server can still persist the change after the user clicked cancel.
For simple admin forms, the 'take only needed fields' approach is preferable to deep cloning because it keeps the form shaped for the UI, not the API response, and avoids the edge cases of `structuredClone`.
Building the form field-by-field is indeed the most stable, but the cost is that it becomes a manually maintained checklist: every time you add an editable field, you have to touch two places—copy it when opening and reassemble it when saving. Miss one spot and this bug recurs. If someone on the team cuts corners and goes straight to deep copy, first be clear about the respective costs. JSON round-trip turns dates into strings, keys with value undefined disappear entirely, NaN and Infinity become null. The browser's native structuredClone throws DataCloneError on functions, DOM nodes, and class instances, and the cloned class instances are just plain objects—methods are gone. Date fields blow up first: they come in as Date, deep copy turns them into strings, and calling toISOString() again before submission yields a TypeError, completely white-screening the UI. Let me add a fifth check to your four: for the same record, open the edit twice in a row—first change and cancel, then second time open and see if the value you get is the one you just edited. If the editor is a child component using useState(props.row) to receive data, the second time it opens, it still has the first copy.
A flash of insight