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

131
Views
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
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!