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

133
Vistas
Javascript object wrong value

I have a React app, where i have this function being called after a click:

dispatch( {type: 'ADD_TO_CART', payload: { cartProduct: {...props, quantity: 1} }}) 

The types are:

interface ICartProduct extends IProduct { 
    quantity: number
}

interface ICartItem { 
    product: ICartProduct
}

export interface IProduct {
    id: number
    category: string
    description: string
    name: string
    brand: string
    price: number
    linkImg: string
}

export interface IState { 
    isCheckoutResume: boolean
    isAddress: boolean
    isPayment: boolean
    isSuccess: boolean
    isHomePage: boolean
    cart: Array<ICartProduct>
    products: Array<IProduct>
}

Inside the reducer function, i have this:

export const reducer = (state: IState, action: IAction): IState => { 

    switch (action.type){ 

        //other features

        case ('ADD_TO_CART'):
            if(action?.payload?.cartProduct) { 

                console.log(action.payload.cartProduct.quantity) // logs always 1, which is expected 
                console.log(action.payload.cartProduct) // logs always an objects having in the quantity property the double + 2 of the value that is already in the cart for that product, so in the second click for the same product it gives 4 (1*2 + 2), in the third click it is 10 (4*2 + 2)

I can't figure out what is happening, should not these lines logs the same value for quantity since it is the same javascript object?

This is not related to console logging only since the application state for cart is also carrying this behavior.

Thank you.

about 4 years ago · Santiago Gelvez
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