在 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;
}
}
有没有更简洁的方法来做到这一点?
Is there a more terse way to do this?
推荐答案
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屋!
查看全文