Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
React reducer wrong increment number

I have a problem when I dispatch an action that triggers my ADD_TO_CART type. I want to increase the quantity of a specific number added in the cart when user click on it. The numbers I'm getting for quantity are : 1, 3, 5, 7, 9.. But they should be incremented only by 1 like 1, 2, 3, 4, 5. Why am I getting double increments in my if statement when I wanna update state indirectly without mutating it? Any kind of help is highly appreciated.

Here is my code :

switch(action.type){
    case 'ADD_TO_CART':
        let tempCart = [...state.cart];
        let specificIndex = state.cart.findIndex((obj) => obj.id === action.item.id);
        if(specificIndex >= 0){
            tempCart[specificIndex].quantity +=1; // it should increment only by 1 and not by 2
            tempCart[specificIndex].price += action.item.price;
            return{
                ...state,
                cart : tempCart,
            }
        }

And here is my state for the example above:

export const initialState = {
cart: [],
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I suspect this is because you are treating tempCart as mutable (adding to its members directly), when really it is just a reference to (not a copy of) part of state, which should always be treated as immutable. Instead, try something like the following:

switch (action.type) {
  case "ADD_TO_CART":
    let specificIndex = state.cart.findIndex(
      (obj) => obj.id === action.item.id
    );
    if (specificIndex >= 0) {
      return {
        ...state,
        cart: state.cart.map((item, idx) =>
          idx == specificIndex
            ? {
                ...item,
                quantity: item.quantity + 1,
                price: item.price + action.item.price,
              }
            : item
        ),
      };
    }
}

See Immutable Update Patterns for more. Note that, even in this small example, these sorts of updates can quickly become unwieldy. There are helper libraries available that can simplify the process, if you are so inclined.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!