reactjs React还原:如何更新购物车中的产品数量

2hh7jdfx  于 2023-03-08  发布在  React
关注(0)|答案(2)|浏览(148)

我在Redux有一个商店,有两个数组womanProducts和cartProducts:

const defaultState = {
  womanProducts:
    [
       {name: "Mini Skirt", img: "https://static.pullandbear.net/2/photos//2022/I/0/1/p/8399/316/800/8399316800_4_1_8.jpg?t=1656932863233&imwidth=563", price: 35, inCart: 1, id: 1},
       {name: "Basic Jeans", img: "https://static.pullandbear.net/2/photos//2022/I/0/1/p/8685/326/400/8685326400_4_1_8.jpg?t=1657798996961&imwidth=563", price: 39, inCart: 1, id: 2},
       {name: "Fit Dress", img: "https://static.pullandbear.net/2/photos//2022/V/0/1/p/4390/422/611/4390422611_4_1_8.jpg?t=1643722977148&imwidth=563", price: 45, inCart: 1, id: 3},
       {name: "Basic Sweatshirt", img: "https://static.pullandbear.net/2/photos//2021/I/0/1/p/8393/363/485/8393363485_4_1_8.jpg?t=1634212398331&imwidth=563", price: 29, inCart: 1, id: 4}
    ],
 cartProducts: [],
}

然后我分派cartProducts并将女性产品添加到购物车中(Case“ADD_PRODUCTS”用于在购物车中添加产品,Case“REMOVE_FROM_CART”用于从购物车中删除项目)。

const reducer = (state = defaultState, action) => {
   switch(action.type) {
      case "ADD_PRODUCTS":
         return {...state, cartProducts: [...state.cartProducts, action.payload]}
      case "REMOVE_FROM_CART":
         return {...state, cartProducts: state.cartProducts.filter((event) => event.id !== action.payload)}

      case "INCREMENT_PRODUCT":
         return {
            ...state,
            cartProducts: state.cartProducts.map((item) => item.inCart + action.payload)}

      default:
        return state
}

但现在我需要通过单击一个按钮来增加和减少购物车中的产品。
推车组件:

return(
    <div className="cart">
        {cartProducts.length > 0 ? cartProducts.map(item =>
            <div className="cartItem" key={item.id}>
                <img src={item.img} />
                <div className="cartItemInfo">
                    <div className="itemInfo">
                        <h3>{item.name}</h3>
                        <h3>{item.price}$</h3>
                    </div>
                    <div className="quantity">
                        <button onClick={() => decrementProduct()}>-</button>
                        <p>{item.inCart}</p>
                        <button onClick={() => incrementProduct()}>+</button>
                    </div>
                    <div className="itemDelete">
                        <button onClick={() => removeItem(item.id)}><img src={require('../images/icons/trashCanIcon.png')} /></button>
                    </div>
                </div>
            </div>
        ): <h1>Cart is empty</h1>}
        {cartProducts.length > 0 ? <h1 className="totalPrice">Total Price: {totalPrice}$</h1> : <></>}
    </div>
)

以及使乘积递增的函数:

function incrementProduct() {
   dispatch({type: "INCREMENT_PRODUCT", payload: 1})
}
l3zydbqr

l3zydbqr1#

你为所有产品增加产品数量。你可以试试这个代码。

case "INCREMENT_PRODUCT":
         return {
            ...state, 
            cartProducts: state.cartProducts.map((item) => ({
id: item.id, 
inCart: item.id === action.payload.id ? item.inCart + action.payload.increment : item.inCart
}));
9gm1akwq

9gm1akwq2#

首先你应该改变你的有效负载,它需要项目id来决定你增加的是哪一个项目

dispatch({type: "INCREMENT_PRODUCT", payload: {id: 1, increment: 1}})

现在,您可以Map购物车产品;如果项目ID与购物车项目ID相同,则递增或仅返回项目

case "INCREMENT_PRODUCT":
            return {
                ...state,
                cartProducts: state.cartProducts.map((item) => {
                    if(item.id === action.payload.id) {
                        item.inCart += action.payload.increment;
                    }
                    return item;
                })
            }

相关问题