Redux在发送时抛出警告:
Error: A state mutation was detected inside a dispatch,in the path: roundHistory.2.tickets. Take a look at the reducer(s) handling the action {"type":"ARCHIVE_TICKETS","roundId":"575acd8373e574282ef18717","data":[{"_id":"575acd9573e574282ef18718","value":7,"user_id":"574c72156f355fc723ecdbbf","round_id":"575acd8373e574282ef18717","__v":0},{"_id":"575acd9573e574282ef18719","value":9,{"_id":"575acd9573e574282ef1871a","value":8,{"_id":"575acdac73e574282ef1871b","value":19,{"_id":"575ad50c4c17851c12a3ec23","value":29,"user_id":"57522f0b1f08fc4257b9cbc6",{"_id":"575ad50c4c17851c12a3ec24","value":28,{"_id":"575ad
处理ARCHIVE_TICKETS动作的唯一减速器是这样的:
case 'ARCHIVE_TICKETS' : var archive = [...state.roundHistory]; for (var i in archive) { if (archive[i]._id === action.roundId) { archive[i].tickets = action.data; } } return Object.assign({},state,{ roundHistory: archive });
如果我使用[…传播],它怎么能改变状态呢?
解决方法
这里的[… state.roundHistory]类似于[] .concat(state.roundHistory).它正在创建一个新数组,但数组中的对象与state.roundHistory共享.如果你想改变它们,你需要复制每个项目.
您可以使用Object.assign执行此操作,类似于您为返回值执行的操作:
var archive = state.roundHistory.map(value => Object.assign({},value));
或者(正如您在自己的答案中所建议的那样),您可以使用对象扩展表示法:
var archive = state.roundHistory.map(value => {...value});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。