我想一成不变地删除react reducer中的对象条目
obj = {
selections: {}
}
这就是我向选择对象obj添加内容的方式
return {
...state,
selections: {
...state.selections,
[action.data.type]: action.data
}
};
这会给我
selections: {
test: { some funky data }
}
那我什么时候想删除测试?我该怎么做到?
答案 0 :(得分:0)
也许是这样,
this.setState(state => {
return {
...state,
selections: {
...state.selections,
selections: delete state.selections.test
}
}
})
答案 1 :(得分:0)
目前我能做的最好的就是将其设置为空
return {
...state,
selections: {
...state.selections,
[action.data.type]: null
}
};
当我使用Object.assign和Delete时不会触发更新
答案 2 :(得分:0)
只需将状态复制到时间变量,然后删除不需要的变量即可:
//...
case 'REMOVE_PROPERTY':
//here we make a copy of selection object, note that this is a shallow copy
let newSelections = { ...state.selection };
delete newSelections[action.data.type];
return {
...state,
//here we overwrite selections object on a new created object before returning the value
selections: newSelections
};
//...