[英]How do I update the state on an object without changing the properties that do not have a new value?
我有这个减速器。
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.name
和endpoint.hostname
,那么reduce会将未在有效负载中传递的值设置为undefined
。 如何使减速器仅更新动作有效负载中的值,并使那些不在动作有效负载中的值保持不变?
最简单的方法是使用对象传播语法:
case UPDATE_ENDPOINT:
return <IState>state.map(endpoint =>
endpoint.ocid === action.endpoint.ocid
? {
...endpoint,
...action.endpoint
}
: endpoint
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.