How to send previous items in cart such that my updateCart adds new items on top of previous products in cart

I am creating a ecommerce product cart, while adding first order it creates order with createOrder and while adding next products it runs updateCart. I have to send the previous items in the cart while running update cart because currently on adding next product it's not working.

This is my service code:

static createOrder(
    order: Order,
    onSuccess: Function,
    onError: Function,
    onFinal: Function
  ) {
    const orderJson = serialize(Order, order)
    const URL = ApiRoutes.CREATE_ORDER
    axiosInstance
      .post(URL, { "order": orderJson })
      .then((res) => {
        // const data = deserialize(Cart, res.data.order);
        //onSuccess(res.data.success);
      })
      .catch((err) => {
        onError(err);
      });
  }

  static updateCart(
    order: Order,
    orderId: string,
    onSuccess: Function,

    onError: Function,
    onFinal: Function
  ) {
    const orderJson = serialize(Order, order)
    const URL = ApiRoutes.UPDATE_CART.replace(":id", `${orderId}`)
    axiosInstance
      .put(URL, { "order": orderJson })
      .then((res) => {
        // const data = deserialize(Cart, res.data.order);
        //onSuccess(res.data.success);
      })
      .catch((err) => {
        onError(err);
      });
  }

This is how I am calling it:

const addToBasket = () => {
    dispatch({
      type: 'ADD_TO_BASKET',
      item: {
        "id": product.id,
        "title": product.name,
        "price": product.price,
        "image": product.imageUrl,
      },
    });

    // CREATE ORDER ONCE CART IS EMPTY ELSE UPDATE CART

    if (props?.userCart?.orderItems?.length) {
      OrderService.updateCart(
        {
          orderItems: [{

            productId: `${product?.id}`,
            quantity: 1,
            // taxMode:0,
            // taxPercentage:0,
            //discountPercentage: ""
          }]
        },
        props?.userCart?.id,
        () => { },
        () => { },
        () => { },
      )
    } else {
      OrderService.createOrder(
        {
          userId: user?.id,
          storeId: store?.id,
          orderItems: [
            {
              productId: `${product?.id}`,
              quantity: 1,
              taxMode: 0,
              taxPercentage: 0,
              discountPercentage: ""
            }
          ]
        },
        () => { },
        () => { },
        () => { },
      )
    }
  };

Solution 1:

Just add previous products to the end of array with spread operator like ...props.userCart.orderItems

OrderService.updateCart(  
 {
              orderItems: [{
    
                productId: `${product?.id}`,
                quantity: 1,
                // taxMode:0,
                // taxPercentage:0,
                //discountPercentage: ""
              }, ...props.userCart.orderItems]
            },
            props?.userCart?.id,
            () => { },
            () => { },
            () => { },
          )
    }