Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda