Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda