我有一个对象数组,对于一种情况,我想在数组的前面添加一个新对象。通过axios调用获取对象数组,并将其保存为redux状态,然后作为prop传递给组件。 现在,在标志为true的情况下,我想添加一个额外的对象以显示为数组中的第一个对象。因此,我创建了一个临时数组并在那里进行了修改。仍然显示错误:
在调度之间的路径list.notificationsList.0
中检测到状态突变。这可能会导致错误的行为。 (http://redux.js.org/docs/Troubleshooting.html#never-mutate-reducer-arguments)
代码如下
const viewNewReport = {"id":5,"name":"newRprt","description":"DESC"}
if (notification && !missingItemFound) {
let tempNotificationsList = this.props.notificationsList
tempNotificationsList.unshift(viewNewReport)
}
,我使用tempNotificationsList在屏幕上显示。 如何避免错误?
答案 0 :(得分:0)
使用对象解构。对于您的情况:
const { tempNotificationsList } = this.props;
就像每次您必须创建道具或状态的副本一样。