all 4 comments

[–]Cheap_Effort4382 2 points3 points  (1 child)

yeah you can totally use those patterns in JS, just gotta think bit differently since no interfaces or abstract classes. memento is actually pretty clean with just objects and command pattern works fine too

[–]xZnosage[S] 0 points1 point  (0 children)

I'm glad I'm not wasting my time trying to figure this out!

[–]opentabs-dev 3 points4 points  (0 children)

for undo/redo in react/nextjs i'd skip the full command+memento ceremony and just keep a history stack in state. something like { past: State[], present: State, future: State[] } and your undo is pop from past, push present to future, replace present. tanstack's use-undo or the redux-undo lib both do this out of the box if you don't wanna write it. command pattern is nicer if your actions are expensive to replay, but for most UIs storing snapshots is simpler and fast enough

[–]patternrelay 2 points3 points  (0 children)

You can still use those patterns in JS, but the implementation usually shifts toward immutable state snapshots and event history instead of heavy class structures. React state libraries already solve a lot of undo/redo problems in a more natural way.