如何解决无法识别自定义Redux undo reducer状态变量
为了实现自定义的撤消/重做功能,我按照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,future: [] };
case ActionTypes.LAYOUT_SET_MOVES:
let prevIoUsSet = [...past,present];
return { ...state,past: prevIoUsSet,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
}
解决方法
在访问化简器中的状态变量时,我需要使用this.state.past,this.state.present和this.state.future,而我在开关中返回的值使用简单的过去,present定义了新状态和未来的变量。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。