【发布时间】:2022-12-15 16:32:23
【问题描述】:
我正在使用使用状态,希望在 0 时停止计数,它在单击时进入 -1、-2、-3 等(添加到购物车按钮)
<div className="border-2 rounded-lg leading-[23.8px]">
<div className="flex items-start justify-center gap-2 ">
<button onClick={() => setCount(count-1)} className="flex self-center p-2 text-[red] hover:bg-[red] hover:text-white rounded active:bg-red-700">
<IoIosRemove />
</button>
<div className="flex self-center">
<p>{count}</p>
</div>
<button onClick={() => setCount(count +1)} className="flex self-center p-2 text-[red] hover:bg-[red] hover:text-white rounded active:bg-red-700">
<IoIosAdd />
</button>
</div>
</div>
当递减量达到 0 时,我希望任何 if 语句或按钮禁用属性
【问题讨论】:
-
而不是
setCount(count - 1)使用setCount(count < 1 ? 0 : count - 1) -
这可以帮助你:*.com/a/66077238/11095009
-
这回答了你的问题了吗? How to stop negative number with react hooks?
标签: javascript node.js reactjs visual-studio tailwind-css