AI Hints
React · Lesson 27 of 120

Checkboxes and Selects

Other inputs.

⏱ 2 min read▶ Video at 21:03🎓 Free
Watch this lesson in the React courseWatch on YouTube ↗Full React course

Other inputs work the same way, with small differences. Checkboxes use checked instead of value. Selects use value, just like text inputs.

  • Same idea
  • checked for checkboxes
  • value for selects

Delivery options

The select chooses a delivery speed. The checkbox uses checked, and reads e dot target dot checked. The summary updates from both. We pick next day, and tick gift wrap. Run it.

App.jsxjsx
import { useState } from "react";

export default function App() {
  const [speed, setSpeed] = useState("standard");
  const [gift, setGift] = useState(false);

  return (
    <div>
      <select id="speed" value={speed} onChange={e => setSpeed(e.target.value)} style={{ fontSize: 19 }}>
        <option value="standard">Standard (free)</option>
        <option value="next">Next day (£9.99)</option>
      </select>
      <label><input id="gift" type="checkbox" checked={gift} onChange={e => setGift(e.target.checked)} /> Gift wrap</label>
      <p className="box">Delivery: {speed}{gift ? ", gift wrapped" : ""}</p>
    </div>
  );
}
Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#61dafb;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} small{color:crimson;display:block;font-size:16px} .big{font-size:64px;font-weight:800}
Also uses actions.json
[{"select": "#speed", "value": "next"}, {"click": "#gift"}]

Summary

Checkboxes use checked, selects use value, and both update state on change. Next, submitting forms.

Key takeaways
  • checked
  • e.target.checked
  • <select value>
Next up: Submitting Forms — onSubmit