First I already has a working input Form and I can display the value of weight and price correctly.
But I have a question, when I change the Weight and Price, and I set a State value (totalPrice and payment) based on another State (weight and Price), the value doesn't get rendered immediately. I need to click the Button multiple time before the render display a correct value one by one (first click weight and price is correct, then when I click again the totalPrice, then I need to click again for payment to show the correct value).
This is a piece of my code, Thank You.
const [weight, setWeight] = useState(0)
const [pricePerGr, setPricePerGr] = useState(0)
const [totalPrice, setTotalPrice] = useState(0)
const [payment, setPayment] = useState(0)
const calculate = () => {
setWeight(weightInput.value)
setPricePerGr(priceInput.value)
setTotalPrice(weight * pricePerGr)
setPayment(totalPrice * 80 / 100)
}
<Button onClick={calculate}>Calculate</Button>
<div>
weight: {weight}
price per grams: {pricePerGr}
Total Price: {totalPrice}
Payment: {payment}
</div>