DEV Community

Discussion on: Undo/Redo in React Using XState

Collapse
 
robertbroersma profile image
Robert

Hey Rui,

That's a good question!

I think I would go with a top-level kind of history machine like in your last example, to keep the concerns of the machines separated.

One way you could go about this is to implement the Command Pattern with undoable commands.

This works well because your history machine just needs to keep a stack of the commands in memory, as opposed to a stack of ALL previous (and some future) state.

Hope that helps!

Collapse
 
ruiorey profile image
Rui Luís d'Orey

Hey Robert,
thanks for the answer.
Yes one classical way is using Command Pattern, now what I'm trying to get around is how to model that using XState (or not, maybe taking that out of Xstate) in an elegant and safe way.
I will also make this question on spectrum.chat/statecharts to see what other suggestions might come up.
Thanks a lot :)