0

The goal is for the button to trigger a function, but when it surpasses minGroupSizeForDiscount, the if condition doesn't trigger. I suspect this might be due to how asynchronous set functions behave in Next.js. The workaround I found was to subtract -1 to force it into the if condition.

  function handleQuantityChangeForGroup  (type, amount) {
    
    setQuantities(prevQuantities => ({
      ...prevQuantities,
      [type]: Math.max(0, prevQuantities[type] + amount),
      
    }));

 
    const totalQuantity = quantities.adult + quantities.child + quantities.infant;
    // in this case mingroupsize is 6
    if (totalQuantity > minGroupSizeForDiscount-1) {
      setExtrasAdded(prevExtras => ({
        ...prevExtras,
        [type]: prevExtras[type] + amount,
      }));
    } else {
      setExtrasAdded({
        adult: 0,
        child: 0,
        infant: 0,
      });
    }
     

  };

I was adding some products and when i added like 7 i didn't trigger the if

PD: i think that the problem is the asynchronous behaviour of the set stament but i don't know how to solve this problem, the only solution i thinked was to put a -1 inside the if

0

Browse other questions tagged or ask your own question.