How to Increment/Decrement a Value in React/NextJS


Use the following code block to get started:

function GetCount() {
  const [count, setCount] = useState(1);

  const incrementCounter = () => {
    setCount(count+1)
  }
  const decrementCounter = () => {
    if (count>1) setCount(count-1)
  }

  return <div className="_counterWrapper">
    <span className="_dec" onClick={() => decrementCounter()}>-</span>
    <span className="_val">{count}</span>
    <span className="_inc" onClick={() => incrementCounter()}>+</span>
  </div>
}

Then in your HTML code, add it like this:

<GetCount />