0

我有这个减速器。

case UPDATE_ENDPOINT:
  return <IState>state.map(endpoint =>
    endpoint.ocid === action.endpoint.ocid
      ? { 
        ...endpoint, 
        name: action.endpoint.name,
        hostname: action.endpoint.hostname,
        origin: action.endpoint.origin,
        originType: action.endpoint.originType,
        originValidation: action.endpoint.originValidation,
      }
      : endpoint
  );

假设在我的动作有效负载中我只有endpoint.nameand endpoint.hostname,那么减速器会将有效负载中未传递的值设置为undefined。如何让reducer 只更新动作有效负载中的值,而让不在动作有效负载中的值保持不变?

4

1 回答 1

2

最简单的方法是使用对象传播语法:

case UPDATE_ENDPOINT:
  return <IState>state.map(endpoint =>
    endpoint.ocid === action.endpoint.ocid
      ? { 
        ...endpoint, 
        ...action.endpoint
      }
      : endpoint
  );
于 2018-03-20T18:50:00.173 回答