【问题标题】:how can i stop the count at 0我怎样才能停止计数为 0
【发布时间】: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 语句或按钮禁用属性

【问题讨论】:

标签: javascript node.js reactjs visual-studio tailwind-css


【解决方案1】:

尝试以下:

onClick={() => {
if (count > 0) setCount(count-1)
}}

【讨论】: