简体   繁体   English

我正在尝试使用 react useState 挂钩来实现产品数量的增加和减少

[英]I am trying to implement product quantity increment and decrement using react useState hooks

const { price, available, name, menimum } = service;
const [updatedQuantity, setUpdatedQuantity] = useState(menimum)

I am trying to implement useState default value is dynamically.我正在尝试动态实现 useState 默认值。 Minimum works properly console.log when I am set useState minimum value is return NaN当我设置 useState 最小值时,最小值可以正常工作 console.log 返回 NaN

const { price, available, name, menimum } = service;
const [updatedQuantity, setUpdatedQuantity] = useState(menimum)

const handleDecrease = () => {
    setUpdatedQuantity(pre => pre - 1)
}
const handleIncrease = () => {
     setUpdatedQuantity(pre => pre + 1)
}

Here is what your code does:这是您的代码的作用:

  1. Set values to four const variables from the "service" variable.将值设置为“服务”变量中的四个常量变量。
  2. Set the initial state of "setUpdatedQuantity" to "menimum" - which was assigned from the "service" variable.将“setUpdatedQuantity”的初始状态设置为“menimum”——这是从“service”变量分配的。
  3. You declare increment functions.您声明增量函数。

Check the initial values of "service" if it comes with "menimum" value NaN or not.检查“服务”的初始值是否带有“最低”值 NaN。 Would be good if you can put more code for better analysis.如果您可以放置更多代码以进行更好的分析,那就太好了。

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

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