mediumReact#144

Undo/redo reducer

Prompt

Write historyReducer(state, action) over the shape { past: [], present, future: [] }:

  1. { type: 'SET', value } — pushes current present onto past, sets new present, clears future
  2. { type: 'UNDO' } — moves present to future, pops the last past entry into present
  3. { type: 'REDO' } — inverse of UNDO
  4. UNDO with empty past / REDO with empty future return state unchanged
  5. Pure function — no mutation, no side effects

Hints

Solution

Your Code

15 min

Tests

Click Run to test your code