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

Checkbox: checked and onChange

Control checkboxes with checked prop and read e.target.checked in onChange handler.

What You'll Learn

You'll learn how to control a checkbox input using the checked prop and read its value with e.target.checked in the onChange handler.

The Core Pattern Shift

You've already mastered controlled text inputs using value and onChange. Checkboxes work almost identically, but with one crucial difference: instead of the value prop, you use the checked prop to control whether the checkbox is ticked or not.

And when reading the user's interaction, you don't use e.target.value—you use e.target.checked, which is a boolean (true or false).

The Checkbox Pattern

function NewsletterSignup() {
  const [agreed, setAgreed] = useState(false);

  return (
    <label>
      <input 
        type="checkbox"
        checked={agreed}
        onChange={(e) => setAgreed(e.target.checked)}
      />
      I agree to the terms
    </label>
  );
}

Notice what's happening:

  • checked={agreed} — The state drives whether the box appears checked
  • e.target.checked — Gives you a boolean when the user clicks
  • setAgreed(e.target.checked) — Updates state with the new checked status

Why checked Instead of value?

Think of it this way: a checkbox doesn't have a "text value" like an input field. It has a state: on or off, true or false. The checked prop reflects that binary nature. The DOM checkbox element has a .checked property (boolean), not just a .value property (string).

A Common Toggle Pattern

onChange={(e) => setAgreed(e.target.checked)}

This is the standard controlled checkbox pattern. Every click updates your state with the checkbox's new status.

Key Takeaway: Controlled checkboxes use checked (not value) as the controlling prop, and read e.target.checked (a boolean) in onChange to capture user interaction.