Skip to main content

Increment / Decrement Counter

Build a counter with buttons to increase and decrease the count value.
The counter should update immediately when the user clicks the buttons.

Input:  Click "+" three times, then "-" once  
Output: 2
import React, { useState } from "react";

function Counter() {
const [count, setCount] = useState(0);

return (
<div>
<h2>Count: {count}</h2>
<button onClick={() => setCount(count - 1)}>-</button>
<button onClick={() => setCount(count + 1)}>+</button>
</div>
);
}

export default Counter;