0

I'm having trouble implementing a simple counter update in react. I have a quantity state to update my shopify cart. There is an increment and decrement function. In the beginning, the quantity is set to 0. When the add button is clicked, it triggers increment function where the quantity variable updates correctly i.e shows 1. But when I console log it, it shows the initial 0 value, and also causes an error in the backend where 0 item gets added to the cart. However, if I add 2 or more items to the quantity and then click 'buy now' it updates the cart with correct identity and increment or decrement functions as normal. The issue just happens in the very first increment from 0 to 1. Why won't the console log show the updated value and why won't set state actually change the quantity in the backend? How do I go about solving this?

here is some of my code : Component -

const Subblueminal = () => {
const { addItemToCheckout, updateCheckout, checkout } = useContext(Context);
const [quantity, setQuantity] = useState(0)

   function Decrement() {
      setQuantity(prevQuantity => prevQuantity - 1) 
      if (checkout.lineItems.length > 0) {
         updateCheckout(checkout.lineItems[0].id, quantity)
      }
      console.log('sub: ' + quantity)
      }
   
   function Increment() {
      setQuantity(prevQuantity => prevQuantity + 1)
      addItemToCheckout(product.variants[0].id, quantity)
      setError(false)
      console.log('add: ' + quantity)
   }
   
  return (
   <div className="action">
            <div className="quantity-select">
               <button onClick={Decrement} className='sub'>-</button>
               <p className='quantity'>{quantity}</p>
               <button onClick={Increment} className='add'>+</button>
            </div>
            <button onClick={() => {
               if (quantity > 0) {
                  updateCheckout(checkout.lineItems[0].id, quantity)
                  openSizeUpdate()
               }
            }} className="buy">(BUY NOW)</button>
         </div>
)

}

Context -

 addItemToCheckout = async (variantID, quantity) => {
      const itemToAdd = [{
         variantId: variantID,
         quantity: parseInt(quantity)
      }]
      const checkout = await client.checkout.addLineItems(this.state.checkout.id, itemToAdd)
      this.setState({ checkout: checkout })
      console.log(checkout.lineItems);
   }

   updateCheckout = async (variantID, quantity) => {
      const lineItemsToUpdate = [
         {id: variantID, quantity: quantity}
       ];
      const checkout = await client.checkout.updateLineItems(this.state.checkout.id, lineItemsToUpdate)
      this.setState({ checkout: checkout })
      // console.log(checkout.lineItems);
   }
Laiqa Mohid
  • 461
  • 3
  • 14
  • Does this answer your question? [The useState set method is not reflecting a change immediately](https://stackoverflow.com/questions/54069253/the-usestate-set-method-is-not-reflecting-a-change-immediately) – Konrad Jan 01 '23 at 18:38
  • I don't think useEffect is what I want, since I want to trigger state change only when the button is clicked, so not sure if that's the solution – Laiqa Mohid Jan 01 '23 at 18:44
  • You don't need `useEffect`. Just don't try to access `quantity` after it was changed – Konrad Jan 01 '23 at 18:51
  • how would i update the context backend with quantity correctly? when I click on add, I wanted the quantity to update with the new value – Laiqa Mohid Jan 02 '23 at 04:43

0 Answers0