【问题标题】:Redux state set object to objectRedux 状态设置对象到对象
【发布时间】:2019-09-16 12:27:48
【问题描述】:

我的初始状态如下:

let initialState = {
   items: [
      {
        name: 'a'
      },
      {
        name: 'b'
      }
   ],
   otherItems: []
}

我正在尝试复制项目并将其设为 otherItems,以便 otherItems 与项目相同,以防SET_OBJECT

case: SET_OBJECT
  return {
   ...state, 
   otherItems: items
  }

我试过这个但没有奏效。我收到错误项目未定义

【问题讨论】:

  • 能否请您发布您的完整减速器?

标签: javascript reactjs ecmascript-6 redux


【解决方案1】:

你的代码应该是这样的

case: SET_OBJECT: 
  return {
   ...state, 
   otherItems: initialState.items
  }

【讨论】:

    【解决方案2】:

    不要直接使用items,而是使用state.items

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 2020-11-02
      • 1970-01-01
      • 1970-01-01
      • 2019-12-07
      • 2020-02-08
      相关资源
      最近更新 更多