React Redux 无法在状态中将项目添加到列表中 [英] React Redux Cant add item into list in the state

查看:52
本文介绍了React Redux 无法在状态中将项目添加到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

列表的 initialState 运行良好,我也可以在reducre中找到控制台日志.

The initialState for the list is works perfectly, and i can found the console log in the reducre too .

然而它未能在列表中再添加一项.

However its failed to add one more item in the list.

我在调用CLICK_SEARCH"操作后检查了商店
列表中的项目数 (products1) 仅剩 1 个(初始状态为那个).

I have checked the store after i called the "CLICK_SEARCH" action
the number of item in the List (products1) remaining 1 only(initialState that one).

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

推荐答案

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

You're currently replacing whole state. You should do like this:

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"
  }
 ]
}

这篇关于React Redux 无法在状态中将项目添加到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆