TanStackT
TanStack13mo ago
2 replies
sacred-rose

is it possible to use form values as a useMemo dependency ?

so i have this

const calculatedTotal = useMemo(() => {
  
    const values = form.state.values;
    let total = 9; // Base price
    
    if (values.sendingMethod === "pickup") {
      total += 5;
    }
    
    if (values.weight > 10) {
      total += 4;
    }
    
    if (values.numberOfParcels === 2) {
      total += 4;
    }
    
    if (values.deliveryType === "express") {
      total += 1;
    }
    
    return total;
    
}, [form.state.values]);


my problem is that the calculatedTotal doesn't update when a form value changes

and i just have a form with a the calculatedTotal in a div, and i need it's value to update when the user changes sendingMethod foe example.
Was this page helpful?