Course contentsShow
← React
Lesson 458 of 2,39613. Controlled ComponentsFree lesson

Controlled Checkboxes

Checkboxes use checked={state} instead of value, with onChange toggling boolean state.

Controlled Checkboxes

What you'll learn: How to control checkboxes in React using checked instead of value, with onChange toggling boolean state.

Checkboxes work a bit differently from text inputs. Instead of tracking a string in value, they track a boolean in checked. The checkbox is either checked (true) or not (false).

The Checkbox Pattern

For a controlled checkbox:

  1. Create boolean state with useState
  2. Set the checked prop to that state
  3. Use onChange to toggle the state
const [isChecked, setIsChecked] = useState(false);

<input 
  type="checkbox" 
  checked={isChecked}
  onChange={(e) => setIsChecked(e.target.checked)}
/>

Notice e.target.checked instead of e.target.value — checkboxes expose their state as a boolean through the checked property.

Toggling Pattern

A common alternative is toggling the previous state:

<input 
  type="checkbox" 
  checked={isAgree}
  onChange={() => setIsAgree(prev => !prev)}
/>

This doesn't read from the event at all — it just flips whatever the current state is.

Why This Matters

Think of a checkbox like a light switch: it's either on or off, not holding a text value. The checked prop reflects that binary nature. Just like controlled text inputs can't change without your permission, controlled checkboxes can't toggle unless you update the state in onChange.

Without onChange, the checkbox becomes frozen — clicks do nothing because React keeps re-rendering it with the same checked value.

Key Takeaway: Controlled checkboxes use checked={booleanState} with onChange updating that boolean, typically by reading e.target.checked or toggling prev => !prev.