Skip to content
Rung

12/12

No. 12Hard30 min

useReducer: An Editor with Undo

Mission

Build complex state logic with a reducer that tracks history (undo/redo), keeping all of it out of the UI.

Tasks

  1. 1

    Model the state as { past, present, future }.

  2. 2

    Handle SET_TEXT: push the current present into past, set the new present, and clear future.

  3. 3

    Handle UNDO: move present to the front of future and take the last item of past as the new present.

  4. 4

    Handle REDO: the reverse of UNDO.

  5. 5

    Return the same state when there is nothing to undo or redo.

  6. 6

    Never mutate: build new arrays and objects every time.