React Redux 无法在状态中将项目添加到列表中

编程入门 行业动态 更新时间:2024-10-10 13:22:54
本文介绍了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 无法在状态中将项目添加到列表中

本文发布于:2023-11-12 11:15:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581341.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   状态   项目   列表中   React

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!