我从redux开始,我想对我的文件进行以下修改
州:
由此:
state = {
loaded: true,
fetching false,
byId: {
"employeeID1": {
id: "employeeID1",
name: "Steve"
},
"employeeID2": {
id: "employeeID2",
name: "Susan"
}
}
}
对此:
{
loaded: true,
fetching false,
byId: {
"employeeID1": {
id: "employeeID1",
name: "Steve",
data: data // <---- add a new property
},
"employeeID2": {
id: "employeeID2",
name: "Susan"
}
}
}
此
const modifEmployee = {...state.byId["employeeID1"], data: data}
将为我提供数据的修改后的员工。但是,如何在保持其他成员不变的情况下在byId中添加修改后的雇员?
最佳答案
您可以使用传播语法执行以下操作:
{
...state,
byId: {
...state.byId,
employeeID1: { ...state.byId.employeeID1, data }
}
}
如果
"employeeID1"
值是从变量employeeId
获取的,则可以使用computed property names:{
...state,
byId: {
...state.byId,
[employeeId]: { ...state.byId[employeeId], data }
}
}