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

79
Vistas
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
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