有时减速机会有点混乱:

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的一部分

它可能会通过non-standardised yet properties spread syntax:

return {
    ...state,
    notificationBar: {
        ...state.notificationBar,
        open: true,
    },
};

Reactjs相关问答推荐

为什么我无法访问窗口事件处理程序中的状态?

Firebase删除UserWithEmail AndPassword仅创建匿名用户

CreateBrowserRouter将props 传递给父组件以验证权限

通过单击具有此值的按钮将值添加到数组对象键

如何在重新图表中更改悬停时的条形填充 colored颜色 ?

Reaction Hook';useEffect&;有条件呼叫问题:

捕获表单数据时的Reactjs问题

在动态React组件中删除事件侦听器

在transformResponse中使用来自其他查询的缓存

子路径上未定义useMatches句柄

在react上隐藏源映射

在迭代状态时无法读取未定义的属性(读取 map )

.populate() 方法在 mongodb 中不起作用

FabricJS反序列化问题

解决在React测试库中的act()警告

如何更新redux状态存在的输入框

如何在 JS 中绘制具有不同笔画宽度的样条线

如何使图标适合 react-bootstrap 中的行元素

setState 改变对象引用

为什么 state redux-toolkit 是代理?