亲,欢迎光临欧方源码网,本站为您提供优质的资源与服务! 按 ctrl + D 加入收藏,下次访问更方便。
当前位置:首页 > 站长学院 > 前端 > Redux修改State的深层对象

Redux修改State的深层对象

前端
欧方源码 2023-01-25 1821次

希望修改state.notificatinBar.open 

const initialState = {
    notificationBar: {
        open: false,
    },
};


方法一:使用Object.assign()

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;
  }
}


方法二:使用ES6的三点语法

 
export default function (state = initialState, action) {
  switch (action.type) {
    case actions.LAYOUT_NOTIFICATIONBAR_OPEN:
      return {
        ...state,
        notificationBar: {
            ...state.notificationBar,
            open: true,
        },
    };
    default:
      return state;
  }
}
声明: 本站资源来自网络或会员自行发布,请勿用于非法及商业用途,如果侵犯了您的权益请与我们联系!
推荐模板
QQ在线咨询