我有一个像这样在新的“上下文API”中创建的对象数组...
NTSTATUS
NTAPI
SeLocateProcessImageName(
PEPROCESS Process,
PUNICODE_STRING *ImageName
);
我想在“ reducer”中创建一个动作,该动作使我可以基于将其作为有效载荷传递给该动作的ID来编辑每个联系人的“ show”属性,该怎么做?
答案 0 :(得分:1)
要避免数组突变并在编辑联系人时保留元素位置,您可以执行以下操作:
10/17/2017 = 2
10/18/2017 = 11
10/19/2017 = 3
10/20/2017 = 10
10/21/2017 = 11
10/22/2017 = 3
10/23/2017 = 8
10/24/2017 = 6
10/25/2017 = 10
答案 1 :(得分:0)
您可以找到联系人,通过使用spread
避免突变,设置show
的新值:
case "EDIT_CONTACT":
const { id, show } = action.payload; // Assume id and show are in action.payload
const contact = { ...state.contacts.find(c => c.id === id), show };
return {
...state,
contacts: [...state.contacts.filter(c => c.id !== id), contact]
};
如果订单很重要:
const { id, show } = action.payload;
const contact = { ...state.contacts.find(c => c.id === id), show };
const index = state.contacts.findIndex(c => c.id === id);
return {
...state,
contacts = [ ...state.contacts.slice(0, index), contact, ...state.contacts.slice(index + 1)];
}