无法使用反应更新插件更新 redux 状态
unable to update redux state using react update addon
下面是我的减速器状态 console.log
state:{getMessagesRequest: false,projects:[]}
项目数组具有以下结构的对象
{messages:Array[0],userid:'foo',username:'bar'}
reducer 使用以下情况将消息对象推送到消息数组中:
case GET_PROJECT_MESSAGES_SUCCESS:
console.log('GET_PROJECT_MESSAGES_SUCCESS invoked: ',action)
let target = state.projects.findIndex((project) => {
return project.project_id == action.project_id
})
action.data.map((message) => {
console.log('TARGET IS : ',target)
console.log('MESSSAGE IS: ',message)
return update(state,{
projects:{
[target]:{
messages:{
$push:[message]
}
}
}
})
})
console.log 显示目标 (target=0) 和消息(对象)的有效值
但是reducer显示以下错误
Uncaught TypeError: Cannot read property 'messages' of undefined
这表明它打破了 [target] .. 但 target 具有正确的值,所以我不确定问题可能出在哪里。
我有一个额外的 reducer 几乎在做类似的事情,但我只有一个消息对象要推送到项目数组。 reducer 在这种情况下工作:
case NEW_CHAT_MESSAGE: //use parseInt instead of ==
target = state.projects.findIndex((project) => {
return project.project_id == action.data.projectid
})
return update(state,{
projects:{
[target]:{
messages:{
$push:[action.data]
}
}
}
})
action.data
是一个对象。第一个 reducer 处理对象数组 - 因此是 map 函数。
问题出在哪里?
update :这很奇怪,但是一旦 GET_PROJECT_MESSAGES_SUCCESS
执行完成,第二个减速器 NEW_CHAT_MESSAGE
也会被调用。错误由 new_chat reducer 发送。状态仍然没有得到更新。这可能是由于第一个减速器的多个 returns 造成的问题吗?(因为我正在遍历对象?)
最后这样做而不是遍历数组
return update(state,{
projects:{
[target]:{
messages:{
$set:action.data
}
}
}
})
下面是我的减速器状态 console.log
state:{getMessagesRequest: false,projects:[]}
项目数组具有以下结构的对象
{messages:Array[0],userid:'foo',username:'bar'}
reducer 使用以下情况将消息对象推送到消息数组中:
case GET_PROJECT_MESSAGES_SUCCESS:
console.log('GET_PROJECT_MESSAGES_SUCCESS invoked: ',action)
let target = state.projects.findIndex((project) => {
return project.project_id == action.project_id
})
action.data.map((message) => {
console.log('TARGET IS : ',target)
console.log('MESSSAGE IS: ',message)
return update(state,{
projects:{
[target]:{
messages:{
$push:[message]
}
}
}
})
})
console.log 显示目标 (target=0) 和消息(对象)的有效值
但是reducer显示以下错误
Uncaught TypeError: Cannot read property 'messages' of undefined
这表明它打破了 [target] .. 但 target 具有正确的值,所以我不确定问题可能出在哪里。
我有一个额外的 reducer 几乎在做类似的事情,但我只有一个消息对象要推送到项目数组。 reducer 在这种情况下工作:
case NEW_CHAT_MESSAGE: //use parseInt instead of ==
target = state.projects.findIndex((project) => {
return project.project_id == action.data.projectid
})
return update(state,{
projects:{
[target]:{
messages:{
$push:[action.data]
}
}
}
})
action.data
是一个对象。第一个 reducer 处理对象数组 - 因此是 map 函数。
问题出在哪里?
update :这很奇怪,但是一旦 GET_PROJECT_MESSAGES_SUCCESS
执行完成,第二个减速器 NEW_CHAT_MESSAGE
也会被调用。错误由 new_chat reducer 发送。状态仍然没有得到更新。这可能是由于第一个减速器的多个 returns 造成的问题吗?(因为我正在遍历对象?)
最后这样做而不是遍历数组
return update(state,{
projects:{
[target]:{
messages:{
$set:action.data
}
}
}
})