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

77
Views
how to setState and update an object within an array

i need your help please :-)

    state = {
        order: {
            createdAt: Date.now(),
            products: [
                {
                    leftInStock: "12",
                    productName: "בלה",
                    productPrice: "120",
                    qtyInOrder: 0
                }
            ],
            totalPrice: 0,
            miniClient: {
                id: '',
                fullName: ''
            }
        },
        client: null,
        isClientSelected: true
    }

and this is the function that should preform the update on the object (need to update qtyInOrder).

    setQtyForProduct = (product, name) => {
        if (name === '+') product.qtyInOrder++
        if (name === '-') {
            if (product.qtyInOrder === 1) return
            else product.qtyInOrder--
        }
        const { products } = this.state.order
        this.setState({ order: { ...this.state.order, products: [...products, product] } }, () => { this.getOrderPrice() })
    }

this is how i pass the values

<div className="order-selected-product-list">
   {this.state.order.products.map((product, _id) => {
      return (
         <div key={_id} className="order-product">
            <div className="order-selected-product-card">
               <div>{product.productName}</div>
               <div>{product.qtyInOrder}</div>
            </div>
            <div className="order-products-qty-btns">
               <div onClick={() => this.setQtyForProduct(product, "+")}>+</div>
               <div onClick={() => this.removeProductFromOrder(product)}>הסר</div>
               <div onClick={() => this.setQtyForProduct(product, "-")}>-</div>
            </div>
         </div>
             )
        })}
</div>

i do get the values right , the only thing i caould not manage to do is update the

I know that is something wrong with my setState function cause it adds the products instead of updating the key in it

about 4 years ago · Juan Pablo Isaza
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!