更新/更短的方式来更新Redux中的嵌套状态? [英] Cleaner/shorter way to update nested state in Redux?
本文介绍了更新/更短的方式来更新Redux中的嵌套状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有时候减速器会变得混乱:
Sometimes reducers get kind of messy:
const initialState = {
notificationBar: {
open: false,
},
};
export default function (state = initialState, action) {
switch (action.type) {
case actions.LAYOUT_NOTIFICATIONBAR_OPEN:
return Object.assign({}, state, {
// TODO: Find a cleaner way to do this!
notificationBar: Object.assign({}, state.notificationBar, {
open: true,
}),
});
default:
return state;
}
}
有更简洁的方法吗?
推荐答案
UPD :它现在是ES2018的一部分
UPD: it's now a part of the ES2018
可能会通过 非标准化但属性扩展语法:
It might be slightly improved via a non-standardised yet properties spread syntax:
return {
...state,
notificationBar: {
...state.notificationBar,
open: true,
},
};
这篇关于更新/更短的方式来更新Redux中的嵌套状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文