简体   繁体   English

从 React Hooks 中的单选按钮输入计算

[英]Calculations from radio button inputs in React Hooks

My expectation is calcualate total price according to user inputted values.我的期望是根据用户输入的值计算总价。 Total price is depend on "ToopingPrice", "Size" and "Qty".总价取决于“ToopingPrice”、“Size”和“Qty”。 In My code, total price is not calculating.在我的代码中,不计算总价。 Consider that I am still beginner level in React.考虑到我仍然是 React 的初级水平。

  const [quantity, setQuentity] = useState(1)
  const [size, setSize] = useState('medium')
  const [sizePrice, setSizePrice] = useState(product.mediumPrice)
  const [topping, setTopping] = useState('none')
  const [toppingPrice, setToppingPrice] = useState(0)
  const [totPrice, setTotPrice] = useState(product.mediumPrice)

Total price calculating function总价计算 function

    const calcTotPrice = () => {
    alert.success(product.smallPrice);
    const FtotPrice = (sizePrice + toppingPrice)*quantity
    setTotPrice(FtotPrice)
    alert.success("calcTotal working");
  }

This is my radio buttons.这是我的单选按钮。 I am not sure is it correct way to put onClick method like below.我不确定像下面这样放置 onClick 方法是否正确。

  <div className="selectButton">
   <div className="radio-container">
      <input id="500g" name="size" type="radio" defaultValue="500g"  onClick={size => setSize('small')} onClick={sizePrice => setSizePrice(product.smallPrice)} onClick={calcTotPrice}/>
      <label htmlFor="500g">500 g</label>
      <input defaultChecked id="1Kg" name="size" type="radio" defaultValue="1Kg"  onClick={size => setSize('medium')} onClick={sizePrice => setSizePrice(product.mediumPrice)} onClick={calcTotPrice}/>
      <label htmlFor="1Kg">1 Kg</label>
      <input id="2Kg" name="size" type="radio" defaultValue="2Kg"  onClick={size => setSize('large')} onClick={sizePrice => setSizePrice(product.largelPrice)} onClick={calcTotPrice}/>
      <label htmlFor="2Kg">2 Kg</label>
   </div>
</div>
<div className="selectButton">
   <div className="radio-container2">
      <input defaultChecked id="0" name="topping" type="radio" defaultValue="0"  onClick={topping => setTopping('None')} onClick={toppingPrice => setToppingPrice(0)} onClick={calcTotPrice}/>
      <label htmlFor="0">None</label><br />
      <input id="1" name="topping" type="radio" defaultValue="1"  onClick={topping => setTopping('Fresh Fruit')} onClick={toppingPrice => setToppingPrice(product.freshFruitToppingPrice)} onClick={calcTotPrice}/>
      <label htmlFor="1">Fresh Fruit</label><br />
      <input id="2" name="topping" type="radio" defaultValue="2"  onClick={topping => setTopping('Candies & Cashew Nut')}onClick={toppingPrice => setToppingPrice(product.chocolateCandiesAndCashewNutToppingPrice)} onClick={calcTotPrice} />
      <label htmlFor="2">Candies & Cashew Nut</label><br />
      <input id="3" name="topping" type="radio" defaultValue="3"  onClick={topping => setTopping('Moldable Fondan')} onClick={toppingPrice => setToppingPrice(product.moldableFondanToppingPrice)} onClick={calcTotPrice}/>
      <label htmlFor="3">Moldable Fondan</label>
   </div>
</div>

Any help are much appreciated.非常感谢任何帮助。

Are you getting NaN ?你得到NaN了吗? because it seems to me that your problem is providing the wrong type of data for the calculation.因为在我看来,您的问题是为计算提供了错误类型的数据。 are sizePrice , toppingPrice , and quantity numbers? sizePricetoppingPricequantity是多少?

I think your quantity might be a string or something.我认为你的quantity可能是一个字符串或其他东西。

Also, why are you having this many onClicks on a single input?另外,为什么您在单个输入上有这么多的onClicks次数? :D :D

Your 'calcTotPrice' handler should be an effect hook .您的“calcTotPrice”处理程序应该是一个效果挂钩 Try尝试

useEffect( () => {
     alert.success(product.smallPrice);
    const FtotPrice = (sizePrice + toppingPrice)*quantity
    setTotPrice(FtotPrice)
    alert.success("calcTotal working");
},[product, sizePrice, quantity, setTotPrice]);

I would suggest to move onClick logic into 1 separate function, and call it with different params.我建议将onClick逻辑移动到 1 个单独的 function 中,并使用不同的参数调用它。

First input will look like this:第一个输入将如下所示:

<input id="1" name="topping" type="radio" defaultValue="1"  onClick={() => recalculate("Fresh Fruit", product.freshFruitToppingPrice)}/>

And the function will look like that: function 看起来像这样:

const recalculate = (toppingName, toppingPrice) => {
    setTopping(toppingName);
    setToppingPrice(toppingPrice);
    calcTotPrice();
}

Thats the first step, then simply we can debug this code.这是第一步,然后我们就可以调试这段代码了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM