redux 状态的正确初始值应该是多少

What should be the correct inital value for redux state

我想使用 Redux 创建 'add to cart' 功能,所以我定义了一个 reducer 函数,它接受状态和动作,其中为状态提供默认值。

export const defaultValue= {
  products: [
    {
      id: "",
      title: "",
      description: "",
      image: "",
      price: 0,
      amount: 0,
    },
  ]
};

export const manageCart = (
  state = defaultValue,
  action: { type: string; payload: any}
) => {
  switch (action.type) {
    case actionConstants.ADD_TO_CARTS:
      {
       // ADD to card
      }
  break;
    case actionConstants.REMOVE_FROM_CART: {
      // Remove from card     
     }
 default:
      return state;
}
}

所以默认情况下,我在购物车上看到这个空白产品,这让我的逻辑进一步混乱。由于必须向状态提供初始值,我应该做些什么来摆脱这个问题。

So by default I'm seeing this blank product on my cart which is messing up my logic moving further.

因为你在默认状态下传递那个空白产品!

只需传递一个空数组即可

export const defaultValue= {
  products: []
};