Full answer
Every form field has the same two parts in a controlled form: a value from state and an onChange that updates it. The details differ by type.
Text, email and textarea use value and event.target.value. In React, textarea takes value as well, not child text.
A checkbox is on or off. It uses checked={values.newsletter} and reads event.target.checked, which is a boolean. Using value for a checkbox is a common mistake.
Radio buttons are a group that shares one name. Each radio has its own value, and checked={values.slot === 'evening'} marks the active one. State stores the selected value as a string.
A select element takes value on the select itself, and the options carry only their own value. This is different from HTML, where selected is placed on an option. For multiple selection, set multiple and make value an array.
One handler can serve all fields. Give each input a name equal to the key in state. The handler picks the right property based on type: const value = type === 'checkbox' ? checked : value, then uses a computed property key. When updates may overlap, use the functional form of the setter so it does not rely on an old copy.
For large forms, the many re-renders per keystroke can matter. Uncontrolled inputs with FormData, or a library such as React Hook Form, avoid them. For small forms, the one-handler pattern is clear and enough.
The example combines all four field types.
import { useState } from 'react';
export default function SignupForm() {
const [values, setValues] = useState({ name: '', newsletter: false, slot: 'morning', course: 'science' });
function handleChange(event) {
const { name, type, value, checked } = event.target;
setValues((current) => ({ ...current, [name]: type === 'checkbox' ? checked : value }));
}
return (
<form>
<label>
Name <input name="name" value={values.name} onChange={handleChange} />
</label>
<label>
<input type="checkbox" name="newsletter" checked={values.newsletter} onChange={handleChange} /> Newsletter
</label>
<fieldset>
<legend>Batch</legend>
{['morning', 'evening'].map((slot) => (
<label key={slot}>
<input type="radio" name="slot" value={slot} checked={values.slot === slot} onChange={handleChange} /> {slot}
</label>
))}
</fieldset>
<label>
Course
<select name="course" value={values.course} onChange={handleChange}>
<option value="science">Science</option>
<option value="maths">Maths</option>
</select>
</label>
<pre>{JSON.stringify(values)}</pre>
</form>
);
}The check typed a name, ticked the newsletter box, picked the evening batch and chose Maths. The pre element shows the state, rendered result: <form><label>Name <input value="Asha" name="name"></label><label><input type="checkbox" name="newsletter"> Newsletter</label><fieldset><legend>Batch</legend><label><input type="radio" value="morning" checked="" name="slot"> morning</label><label><input type="radio" value="evening" name="slot"> evening</label></fieldset><label>Course<select name="course"><option value="science">Science</option><option value="maths">Maths</option></select></label><pre>{"name":"Asha","newsletter":true,"slot":"evening","course":"maths"}</pre></form>
Key points
- Controlled fields need a value from state and an onChange.
- Checkboxes use checked and event.target.checked. Radios compare checked against the stored value.
- Select uses value on the select element, not selected on options.
- Use name plus a computed key for one handler across fields.
- Large forms may benefit from uncontrolled inputs or a form library.
Common mistakes
- Reading event.target.value for a checkbox and getting the string "on".
- Putting selected on an option instead of value on the select.
- Giving radios different names, so they are not a group.
- Mutating the state object instead of copying it.
- Forgetting name attributes when using one shared handler.
Follow-ups
- How would you handle a select with multiple choices?
- How would you validate this form on submit?
- When would you switch to uncontrolled inputs or React Hook Form?
Tips
- Mention the checkbox checked detail. It is the usual slip.
- Use one example with all four types. It shows you know the differences.
- Say you would reach for a library when forms grow.