0
export const initialState = {
  basket: [],
};

// reducer takes care about adding and removing items from the basket

// selector
export const getBasketTotal = (basket) =>
  basket?.reduce((amount, item) => item.price + amount, 0);

// the above code helps us tally the total amount of products

const reducer = (state, action) => {
  switch (action.type) {
    case "ADD TO BASKET":
      return {
        ...state,
        basket: [...state.basket, action.items],
      };
    // the above code helps us to push items to the basket with current elemnts in the basket
    default:
      return state;
  }
};
export default reducer;

在此处输入图像描述

它显示的错误在 export const getBasketTotal 中,它显示的错误是匿名函数

4

1 回答 1

0

选择器通常接收整个状态对象。我怀疑你想访问一个state.basket.

export const getBasketTotal = (state) =>
  state.basket?.reduce((amount, item) => item.price + amount, 0);

对我来说,其他看起来有点可疑的是"ADD TO BASKET"动作:

basket: [...state.basket, action.items],

action.itemsIMO 意味着多个项目,所以我认为您可能希望将项目分散到basket数组中。

basket: [...state.basket, ...action.items],
于 2021-06-02T07:28:45.850 回答