为了实现自定义的撤消/重做功能,我按照https://redux.js.org/recipes/implementing-undo-history中的说明进行了修改,以适合我自己的减速器。现在,当我尝试运行代码时,我得到的过去,现在和将来的错误都没有定义。该错误不会在最初的情况下出现,而只会在ActionTypes.LAYOUT_SET_MOVES
,ActionTypes.LAYOUT_UNDO_MOVES
和ActionTypes.LAYOUT_REDO_MOVES
情况下出现。
我已经搜索了代码并将状态变量与其他reducer进行了比较,但没有发现明显的原因。如果您能指出正确的方向,我将不胜感激。
下面是完整的减速器代码。
谢谢。
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
}
答案 0 :(得分:0)
在访问化简器中的状态变量时,我需要使用this.state.past,this.state.present和this.state.future,而我在开关中返回的值使用简单的过去,present定义了新状态和未来的变量。