React Redux Cant将项目添加到状态列表中

时间:2018-08-27 09:19:12

标签: reactjs redux

列表的initialState非常完美, 而且我也可以在控制台中找到控制台日志。

但是它未能在列表中再添加一项。

调用“ CLICK_SEARCH”操作后,我已经检查了商店
列表(products1)中的项目数仅剩1个(initialState表示一个)。

initialState3

 var initialState3 = {
        products1:[{
             id: "123",
              abbreviation: "123",
              case_no: "123",
              created_dt: "31/01/2018",
              last_updated: "11:43:45"

          }]
        }

减速器

function ReducersForSeach(state = initialState3, action) {
      switch(action.type) {
          case 'CLICK_SEARCH': {

          console.log("search action found");

           return [ ...state,
                    {
                       id: "123",
                       abbreviation: "123",
                       case_no: "123",
                       created_dt: "31/01/2018",
                       last_updated: "11:43:45"
                     }
                   ]

          }
          default :{
              return state
          }
      }
    }

1 个答案:

答案 0 :(得分:3)

您当前正在替换整个状态。您应该这样做:

return {
 ...state,
 products1: [...state.products1,
  { // I hope, you'll merge action.payload later
   id: "123",
   abbreviation: "123",
   case_no: "123",
   created_dt: "31/01/2018",
   last_updated: "11:43:45"
  }
 ]
}