简体   繁体   English

反应本地添加到购物车数量和总价问题

[英]React native add to cart quantity and total price issue

Facing an issue where my cart is not updating the quantity and total price correctly after 2 quantities. 遇到我的购物车在2个数量后不能正确更新数量和总价的问题。

Using redux action during my ADD_TO_CART on press button, I will assign a new element into object of quantity:1 在按下按钮的ADD_TO_CART期间使用redux动作,我将向数量为1的对象分配一个新元素

export function addToCart(card){
    return (dispatch) => {
        card.quantity = 1;
        dispatch({type: ADD_TO_CART, data:card});
        console.log(card);
    };
}

On my reducer, here is the function 在我的减速器上,这是功能

let cartState = { data: [], totalPrice: 0 };
const cartReducer = (state = cartState, action) => {
            switch (action.type) {
                case ADD_TO_CART:
                let totalPriceCart = 0;
                let checkforDuplicate = state.data.some(function (a){
                    if(a.sno === action.data.sno){
                        return true;
                    }
                });

                if(checkforDuplicate){
                    for (let i in state.data) {
                        if (state.data[i].sno === action.data.sno) {
                            state.data[i].quantity = action.data.quantity+1;
                            break;
                        }
                    }
                    for(let e in state.data){
                        totalPriceCart = totalPriceCart + (state.data[e].price*state.data[e].quantity);
                    }
                    return  {data: state.data, totalPrice: totalPriceCart };
                }

                let cloneArr = state.data.concat(action.data);
                for(let i in cloneArr){
                    totalPriceCart = totalPriceCart+(cloneArr[i].price*cloneArr[i].quantity);
                }

                return {...state, data : cloneArr, totalPrice: totalPriceCart }
                default:
                return state;
            }
        };

What am I doing here in the reducer is, 我在减速器中在做什么,

  • If there is a duplicate item from adding to cart action object (detect by sno), will not concat into the main state array but update the quantity of existing item in the state 如果存在添加到购物车操作对象中的重复项目(由sno检测),则不会合并到主状态数组中,但会更新该状态中的现有项目数量
  • Else will concat the array 否则将合并数组

  • At the end, will count the total price of the whole item in state and pass it back to my cart to show the total based on the quantity*price 最后,将计算整个状态下的总价格并将其传递回我的购物车,以基于数量*价格显示总价格

Issue is that, currently, my code only shows until 2x, when adding the same item for 3rd time, the quantity will stay at 2x and the price is not calculating correctly. 问题是,当前,我的代码仅显示到2倍,当第三次添加同一商品时,数量将保持为2倍,并且价格计算不正确。

you should mutate quantity of the state.data 您应该更改state.data的数量

if (checkforDuplicate) {

    for (let i in state.data) {
        if (state.data[i].sno === action.data.sno) {
            state.data[i].quantity = state.data.quantity + 1;
            break;
        }
    }
    for (let e in state.data) {
        totalPriceCart = totalPriceCart + (state.data[e].price * state.data[e].quantity);
    }
    return { data: state.data, totalPrice: totalPriceCart };
}

mutate using ==== Array.map()======= 使用====进行变异Array.map()=======

    let mutatedArr = state.data.map((item)=> {
        if (item.sno === action.data.sno) {
            item.quantity += 1;
        }
       return item;
    });
let totalPriceCart = 0;
 mutatedArr.forEach((item)=>{
totalPriceCart+ =  (item.price * item.quantity)
    })
    return { data: mutatedArr, totalPrice: totalPriceCart };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM