【发布时间】:2017-11-04 22:21:43
【问题描述】:
我知道以前有人问过这个问题,但几乎所有人都在谈论 OP 直接改变状态,我试图避免使用像扩展运算符这样的技术(在对象和数组中),但尽管如此其中我收到以下错误:
Uncaught Error: A state mutation was detected between dispatches, in the path `courses.0`. This may cause incorrect behavior. (http://redux.js.org/docs/Troubleshooting.html#never-mutate-reducer-arguments)
at invariant (eval at <anonymous> (bundle.js:922), <anonymous>:40:15)
at eval (eval at <anonymous> (bundle.js:3825), <anonymous>:50:36)
at eval (eval at <anonymous> (bundle.js:3846), <anonymous>:14:16)
at dispatch (eval at <anonymous> (bundle.js:3853), <anonymous>:37:18)
at eval (eval at <anonymous> (bundle.js:1151), <anonymous>:63:5)
at eval (eval at <anonymous> (bundle.js:3846), <anonymous>:11:18)
at Object.eval [as saveCourse] (eval at <anonymous> (bundle.js:3860), <anonymous>:4:12)
at Object.ManageCoursePage._this.saveCourse [as onSave] (eval at <anonymous> (bundle.js:2080), <anonymous>:134:27)
at CourseForm._this.onHandleSave (eval at <anonymous> (bundle.js:2052), <anonymous>:93:19)
at Object.ReactErrorUtils.invokeGuardedCallback (eval at <anonymous> (bundle.js:1301), <anonymous>:69:16)
我阅读了the article that the error points to,正如它所暗示的那样,我检查了我是否在减速器中手动改变了状态,但我看不到可能发生的情况:
const courseReducer = (state = initialState.courses, action) => {
switch (action.type) {
case types.LOAD_COURSE_SUCCESS:
return action.courses;
case types.CREATE_COURSE_SUCCESS:
//This action throw the error
debugger;
return [
...state,
Object.assign({}, action.course)
];
case types.UPDATE_COURSE_SUCCESS:
//This action throw the error
debugger;
return [
...state.filter(course => course.id !== action.course.id),
Object.assign({}, action.course)
];
case types.DELETE_COURSE_SUCCESS:
return [...state.filter(course => course.id !== action.courseId)];
default:
return state;
}
};
这里是a sandbox with part of the application,可用于复制错误,奇怪的是,只有在我创建新课程或尝试编辑现有课程时才会不时发生这种错误(操作CREATE_COURSE_SUCCESS 和UPDATE_COURSE_SUCCESS分别在我的@ 987654333@)
如果有人能帮助我找出这个错误的根源,我将不胜感激。
【问题讨论】:
-
我们可以通过
mapStateToProps查看您如何使用您的状态吗? -
当然!这有点乱,但我已经在编辑下添加了。顺便说一句,您可以在我的问题中包含的沙箱中找到相同的代码,只需查找文件
components/course/ManageCoursePage.js
标签: javascript reactjs ecmascript-6 redux react-redux