redux TypeError:state.items是不可重复的

neekobn8  于 2022-11-12  发布在  其他
关注(0)|答案(3)|浏览(137)

我知道在React-Redux中,我总是一次又一次地陷入同一个错误-“TypeError:”我尝试了所有类型的解决方案,但都没有成功。
我有一个电子商务应用程序,有添加到购物篮按钮-当人点击addToBasket按钮的产品被添加到购物篮在我的全局状态和问题-addToBasket按钮是不工作,当我尝试再次添加一个项目,它显示错误.“TypeError:这是我在reducer中的代码--

import { createReducer } from "reduxsauce";
import { BasketTypes } from "store/actions/Basket";

export const INITIAL_STATE = {
  items:[],
};

export const addToBasket = (state = INITIAL_STATE, action) => ({ ...state, items: [...state.items , action.payload]});

export const HANDLERS = {
  [BasketTypes.ADD_TO_BASKET]: addToBasket,

};

export default createReducer(INITIAL_STATE, HANDLERS);

这是我的行动代码:

import { createActions } from "reduxsauce";

const { Types, Creators } = createActions(
  {
    addToBasket: ["payload"],

  },
  {}
); // options - the 2nd parameter is optional

export const BasketTypes = Types;
export default Creators;

这是我得到的错误:

zysjyyx4

zysjyyx41#

我猜你用{}(空对象)调用这个动作。实际上它并不完全是undefined。所以你的INITAL_STATE不起作用

t30tvxxf

t30tvxxf2#

我也遇到过类似的问题。我的state对象中有一个数组&我在createStore中初始化了一个空对象作为第二个参数&在我的代码中调用该数组之前一直在出错。我从createStore中删除了空对象&问题解决了。它似乎被那个覆盖了&我在reducer中的初始化被删除了,所以代码无法访问它。

raogr8fs

raogr8fs3#

Well in my case i was calling state after setting variable into it . 
So it was looking like : 

   patchState({
   Stuff:[...[],...state.Stuff]
   })

So here of our intern work on the bug and he wanted to clean state but with wrong method .
So clear ans would be check the method of state calling might be error while calling so be clear about it . 

And after to it i took it and solved via calling initial state and deified empty array as initial state .

相关问题