0

In order to implement a custom undo/redo function I followed the instructions in https://redux.js.org/recipes/implementing-undo-history and modified them to fit into my own reducer. Now when I attempt to run the code I get the errors that past, present and future are not defined. The error do not appear in the initial cases, but only appear in the ActionTypes.LAYOUT_SET_MOVES, ActionTypes.LAYOUT_UNDO_MOVES and ActionTypes.LAYOUT_REDO_MOVES cases.

I have scoured the code and compared the state variables with other reducers and am finding no apparent cause for this. If you could point me in the correct direction I would appreciate it.

Full reducer code is below.

Thanks.

export const layout_moveData = (state = {
    past: [],
    present: null,
    future: []
}, action) => {

    switch (action.type) {
        case ActionTypes.LAYOUT_DESKS_LOADED:
            return { ...state, present: action.payload, past: [], future: [] };
        case ActionTypes.LAYOUT_RESTORE_ALL:
            return { ...state, present: action.payload, past: [], future: [] };
        case ActionTypes.LAYOUT_SET_MOVES:
            let previousSet = [...past, present];
            return { ...state, past: previousSet, present: action.payload, future: [] };
        case ActionTypes.LAYOUT_UNDO_MOVES:
            if (past.length === 0) return state;
            let previous = past[past.length - 1];
            let newPast = past.slice(0, past.length - 1);
            return { ...state, past: newPast, future: [present, ...future], present: previous };
        case ActionTypes.LAYOUT_REDO_MOVES:
            let next = future[0]
            let newFuture = future.slice(1)
            return { ...state, past: [...past, present], present: next, future: newFuture };
        default:
            return state

    }
Geoff
  • 353
  • 3
  • 19

1 Answers1

0

In accessing the state variables in the reducer I needed to use this.state.past, this.state.present and this.state.future, while the values I return in the switch define the new state with simple past, present and future variables.

Geoff
  • 353
  • 3
  • 19