Redux将值分配给一个对象,该对象已经具有来自另一个Reducer的值

时间:2019-12-04 18:05:01

标签: javascript reactjs redux

所以我有以下代码:

    case LOGIN_SUCCESS:
      return Object.assign({}, state, {
        message: "Login successful",
        cause: null,
        username: action.username,
        fullname: action.fullname,
        credit: action.credit,
        id: action.id,
        topmenutype: action.topmenutype,
        authenticated: true
      });
    case LOCATION_SUCCESS:
      return Object.assign({}, state, {
        location: action.location
      });

所以基本上我想发生的是将location添加到上面的对象中。有可能吗?

2 个答案:

答案 0 :(得分:0)

您可以执行类似上面的操作。

return Object.assign({}, {
        ...state,
        location: action.location
      });

答案 1 :(得分:0)

您无需使用Object.assign并创建对象的深层副本。相反,您可以使用传播运算符。您可以详细了解两者Here

的用法和区别
case LOGIN_SUCCESS:
  return {
    ...state,
    message: "Login successful",
    cause: null,
    username: action.username,
    fullname: action.fullname,
    credit: action.credit,
    id: action.id,
    topmenutype: action.topmenutype,
    authenticated: true
  };
case LOCATION_SUCCESS:
  return {
    ...state,
    location: action.location
  };