没有功能的 Redux Reducer 构建对象

Redux Reducer build object without function

我可以成功地执行以下操作,这使我能够构建一个以用户 ID 作为键的配置文件对象 - 我喜欢这种结构,因为它有助于应用程序的其他部分

function profilesMembersResponseConcat(members, newMember) {
    members[newMember.userid] = newMember
    return  members
}

[C.PROFILES_MEMBERS_RESPONSE]: (state, { payload }) => ({
    ...state,
    profilesMembersResponse: profilesMembersResponseConcat(state.profilesMembersResponse, payload)
}),

问题:有没有办法在不需要函数的情况下做到这一点。我在下面尝试过,但收到一条错误消息:“箭头函数不应 return 赋值。

[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})
}),