Redux状态将对象设置为对象

时间:2019-09-16 12:27:48

标签: javascript reactjs ecmascript-6 redux

我的初始状态如下:

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

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

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

我尝试了这个但没有用。我收到错误未定义项目

2 个答案:

答案 0 :(得分:2)

您的代码应该像这样

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

答案 1 :(得分:0)

使用items代替直接使用state.items