Sub topic 5
Checkboxes, radios, selects and textareas
This chapter assumes you can write a controlled text input with useState, and that you know object spread and destructuring. Text inputs use value. Other form elements follow the same controlled pattern, but each one reads and writes a different property. Using the wrong property is the most common mistake.
The property each element uses
| Element | Property React reads | Value you read in the handler |
|---|---|---|
| `<input type="text">` | `value` | `e.target.value` |
| `<textarea>` | `value` | `e.target.value` |
| `<select>` | `value` on the `<select>` | `e.target.value` |
| `<input type="checkbox">` | `checked` | `e.target.checked` |
| `<input type="radio">` | `checked` on each radio | `e.target.value` |
Checkboxes
A checkbox is on or off, so its state is a boolean. Pass the boolean to checked, and read e.target.checked in the handler. Reading e.target.value on a checkbox returns the string set in its value attribute, which is not what you want here.
import { useState } from 'react';
export default function UpdatesToggle() {
const [subscribe, setSubscribe] = useState(false);
return (
<label>
<input
type="checkbox"
checked={subscribe}
onChange={(e) => setSubscribe(e.target.checked)}
/>
Send me updates ({subscribe ? 'on' : 'off'})
</label>
);
}Radio buttons
Radios work as a group. Give them the same name, and store the selected value in one piece of state. Each radio sets checked by comparing its own value with that state.
import { useState } from 'react';
export default function SizePicker() {
const [size, setSize] = useState('medium');
return (
<fieldset>
<legend>Size</legend>
{['small', 'medium', 'large'].map((option) => (
<label key={option}>
<input
type="radio"
name="size"
value={option}
checked={size === option}
onChange={(e) => setSize(e.target.value)}
/>
{option}
</label>
))}
<p>Selected: {size}</p>
</fieldset>
);
}Selects and textareas
In plain HTML, you mark the chosen option with a selected attribute, and a textarea holds its text as children. React simplifies both. Put value on the <select> itself, not on the options. On a <textarea>, use a value prop and a self-closing or empty element.
import { useState } from 'react';
export default function FeedbackForm() {
const [topic, setTopic] = useState('bug');
const [message, setMessage] = useState('');
return (
<form>
<label>
Topic
<select value={topic} onChange={(e) => setTopic(e.target.value)}>
<option value="bug">Bug report</option>
<option value="idea">Feature idea</option>
</select>
</label>
<label>
Message
<textarea
value={message}
onChange={(e) => setMessage(e.target.value)}
rows={4}
/>
</label>
<p>{message.length} characters</p>
</form>
);
}For a multiple select, add the multiple attribute and pass an array to value. Read the chosen options with Array.from(e.target.selectedOptions, (o) => o.value).
One handler for every type
The shared handler from the previous chapter works for all of these elements if it checks the input type. The name attribute becomes the state key.
function handleChange(e) {
const { name, type, value, checked } = e.target;
setForm((prev) => ({
...prev,
[name]: type === 'checkbox' ? checked : value,
}));
}Radios and textareas fall into the value branch. For radios, name is the shared group name, so the state key holds the selected option.
Common errors
- Setting
valueon a checkbox and expecting it to toggle. Usechecked. - Giving radios different
namevalues. The browser then no longer treats them as one group. - Putting
selectedon an<option>. React warns about this. Usevalueon<select>. - Using
checkedorvaluewithoutonChange. React shows a warning and the field becomes read-only. - Initializing a select with
undefined. The element then switches between uncontrolled and controlled. Start with a valid option value or an empty string.
Exercise
A form has radio buttons named "plan" with values "free" and "pro", and a single state object form with a plan key. What should each radio's checked prop be, and what does the shared handler store when the user clicks "pro"?
Show answer
Each radio uses checked={form.plan === option}, where option is that radio's own value. When the user clicks "pro", the handler takes the value branch and stores plan: 'pro', because the type is "radio" and not "checkbox".
Exercise
Why does a checkbox bound with value={subscribe} and onChange={(e) => setSubscribe(e.target.value)} behave incorrectly?
Show answer
A checkbox's on or off state lives in checked, not value. React does not tie value to the toggle, and e.target.value returns the checkbox's fixed value attribute (by default the string "on"). Use checked={subscribe} and e.target.checked.