Aral Code

State & Events Lesson 10 of 13

React Forms — Controlled Inputs, Checkboxes and Selects

Build forms in React with controlled inputs, one state object for many fields, checkboxes, radio buttons and selects, validation messages and resetting the form.

Forms in React use controlled inputs: each input's value comes from state, and every change updates that state. React always knows exactly what's in the form, which makes validation and submission easy.

One state object for many fields

Example A registration form
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<style>
  form { font-family: sans-serif; padding: 12px; max-width: 360px; }
  label { display: block; margin: 10px 0 4px; font-weight: bold; }
  input[type=text], select { width: 100%; padding: 8px; box-sizing: border-box; }
  .err { color: crimson; font-size: 13px; }
</style>
<script type="text/babel">
  const { useState } = React;
  const empty = { name: "", strand: "STEM", shift: "am", club: false };

  function Registration() {
    const [form, setForm] = useState(empty);
    const [errors, setErrors] = useState({});
    const [saved, setSaved] = useState(null);

    function update(e) {
      const { name, value, type, checked } = e.target;
      setForm({ ...form, [name]: type === "checkbox" ? checked : value });
    }

    function submit(e) {
      e.preventDefault();
      const errs = {};
      if (form.name.trim().length < 2) errs.name = "Please enter your full name.";
      setErrors(errs);
      if (Object.keys(errs).length === 0) {
        setSaved(form);
        setForm(empty);
      }
    }

    return (
      <form onSubmit={submit}>
        <label htmlFor="name">Full name</label>
        <input type="text" id="name" name="name" value={form.name} onChange={update} />
        {errors.name && <p className="err">{errors.name}</p>}

        <label htmlFor="strand">Strand</label>
        <select id="strand" name="strand" value={form.strand} onChange={update}>
          <option>STEM</option><option>ABM</option><option>HUMSS</option><option>TVL-ICT</option>
        </select>

        <label>Shift</label>
        <input type="radio" name="shift" value="am" checked={form.shift === "am"} onChange={update} /> Morning{" "}
        <input type="radio" name="shift" value="pm" checked={form.shift === "pm"} onChange={update} /> Afternoon

        <label>
          <input type="checkbox" name="club" checked={form.club} onChange={update} /> Join the coding club
        </label>

        <button style={{ marginTop: 12 }}>Register</button>
        {saved && <p>✅ Registered {saved.name} ({saved.strand}, {saved.shift.toUpperCase()}{saved.club ? ", coding club" : ""})</p>}
      </form>
    );
  }
  ReactDOM.createRoot(document.getElementById("root")).render(<Registration />);
</script>

How the update function works

  • Every input has a name matching a key in the form state.
  • One update function handles all of them: [name]: value updates the field with that name.
  • Checkboxes use checked (true/false) instead of value.
  • Radio buttons are checked when their value matches the state.
  • After a successful submit, setForm(empty) clears the form.

Controlled input rules

ElementState propRead in onChange
<input>, <textarea>valuee.target.value
<select>valuee.target.value
Checkboxcheckede.target.checked
Radiochecked={state === value}e.target.value

In React, <textarea> uses a value prop instead of text between the tags, and <select> uses value instead of marking an option selected.

Numbers from inputs

Input values are always strings. Convert them when you need numbers:

Example A live grade computation
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<script type="text/babel">
  const { useState } = React;
  function GradeCalc() {
    const [scores, setScores] = useState({ ww: "88", pt: "92", qe: "85" });
    const set = (e) => setScores({ ...scores, [e.target.name]: e.target.value });
    const grade = Number(scores.ww) * 0.25 + Number(scores.pt) * 0.5 + Number(scores.qe) * 0.25;
    return (
      <div style={{ fontFamily: "sans-serif", padding: 12 }}>
        WW <input name="ww" type="number" value={scores.ww} onChange={set} style={{ width: 60 }} />{" "}
        PT <input name="pt" type="number" value={scores.pt} onChange={set} style={{ width: 60 }} />{" "}
        QE <input name="qe" type="number" value={scores.qe} onChange={set} style={{ width: 60 }} />
        <h3>Quarterly grade: {grade.toFixed(2)}</h3>
      </div>
    );
  }
  ReactDOM.createRoot(document.getElementById("root")).render(<GradeCalc />);
</script>

The grade is computed during rendering, so it updates instantly as you type.

Sending the data

In a real app, submit would send the data to a server, for example with fetch("/api/register", { method: "POST", body: JSON.stringify(form) }). The server (PHP, Node.js…) must validate again, because client-side checks can be bypassed.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which prop controls a checkbox?
2. What happens to an input with value but no onChange?
3. What does [name]: value do inside an object?
4. What type is e.target.value from a number input?

By Sir Reyel · Last updated 2026-12-04