History is lost when unmounting / remounting diagram

I’m having trouble with undo manager and remembering history when a component is unmounted / remounted. I’ve included some example code based off of the gojs-react-basic code: Test undo / redo w/ unmount by scottdover · Pull Request #13 · NorthwoodsSoftware/gojs-react-basic · GitHub (I tried to highlight the relevant bits that changed in the PR, since my ide auto-formatted a bunch of things).

If I unmount & remount a diagram, the history information is lost. I’ve also attempted to call this.diagramRef.current.clear() on unmount, and alter initDiagram such that it returns a previously created diagram on remount.

I’m wondering if something else needs to happen to make sure history is maintained even when diagrams move off the screen?

The UndoManager maintains a history of Transactions of ChangedEvents. Each ChangedEvent.object refers to a particular Object that was changed.

If you unmount and then remount, it will recreate all of the Nodes, Links, GraphObjects, et al, so that if you had any ChangedEvents from before, those references would no longer be valid because they would refer to the old Nodes and Links, not the new ones.

If you can avoid re-creating the Diagram unnecessarily, that would be wise and enable you to reuse the UndoManager.