我可以成功完成以下操作,使我可以使用用户标识作为关键字来构建配置文件的对象-我喜欢这种结构,因为它有助于应用程序的其他部分
function profilesMembersResponseConcat(members, newMember) {
members[newMember.userid] = newMember
return members
}
[C.PROFILES_MEMBERS_RESPONSE]: (state, { payload }) => ({
...state,
profilesMembersResponse: profilesMembersResponseConcat(state.profilesMembersResponse, payload)
}),
问题:但是,有没有一种方法不需要功能。我在下面尝试过,但是收到错误消息:“ 箭头功能不应返回赋值。”
[C.PROFILES_MEMBERS_RESPONSE]: (state, { payload }) => ({
...state,
profilesMembersResponse: state.profilesMembersResponse[payload.userid] = payload
}),
您的两个解决方案都在使对象变异。您将需要将任务更改为更多类似的内容:
[C.PROFILES_MEMBERS_RESPONSE]: (state, { payload }) => ({
...state,
profilesMembersResponse: ({...state.profilesMembersResponse, [payload.userid]:payload})
}),