Build a React Counter Component with useState

easy
By FrontendAtlas Editorial · Updated Nov 12, 2026
easy level
React useStateEvent handlersDerived disabled state
Build a React counter component with the useState hook. Initialize the count at zero, wire increment and guarded decrement handlers, disable decrement at zero, and keep the rendered value synchronized with state.

Requirements

  • Render a <Counter /> as the main UI.
  • Display the current count value.
  • Provide '+' and '-' buttons to increment and decrement.
  • Prevent the count from ever going below 0.
  • Include a 'Reset' button that sets the count back to 0.

What the checks look for

The checks find elements by .value, button "-", button "+", button "Reset". Keep these in your markup.

Expected behavior

  • Clicking '+' increases the count by 1.
  • Clicking '-' decreases the count by 1, but never below 0.
  • Clicking 'Reset' sets the count back to 0.
  • The '-' button is disabled when the count is 0.
  • Initial count starts at 0.

Implementation notes

  • Initialize local component state with useState(0).
  • Guard the decrement so it never sets a negative value.
  • Disable decrement at zero so the interface communicates the invariant.
  • Keep everything in a single small component for this exercise.

FAQ

How do you build a counter component in React with useState?

Call useState(0) to store the count, render that state, and update it from increment and decrement button handlers. Guard decrement so the value cannot fall below zero.

How do you prevent a React counter from going below zero?

Guard the decrement so it clamps at zero, then disable the decrement button when count is zero to keep behavior and visual state aligned.

Solution idea

A single count state drives the UI. Increment adds 1, decrement subtracts 1 but clamps at 0. Disabling the minus button at zero communicates the rule to users and prevents invalid negative values. This is a clean example of state-driven UI and controlled updates. Performance & pitfalls: consider edge cases (empty state, rapid clicks), trade-offs in state shape, and accessibility of interactive controls.