[英]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.