hard35 min

Build Undo/Redo

Build a drawing canvas (or text editor) with undo/redo functionality. Maintain a history stack of past states and a future stack for redo. Each action pushes to the history, undo pops from history to future, and redo pops from future back to history. This is a classic application of the Command pattern in UI.

useReducerstate machinesimmutable updateskeyboard shortcuts
  • Canvas starts empty with disabled undo/redo
  • Clicking the canvas adds a circle
  • Undo removes the last circle
  • Redo restores an undone circle
  • New action clears the redo stack
Your Code
Loading editor...