Skip to content
Core ConceptsChapter 12 / 37Beginner4 min read

Forms & Controlled Inputs

Learn how to build React forms with controlled inputs, value and onChange, checkboxes, selects, submit handling and an intro to React 19 form actions.

On this page

1. What are forms and controlled inputs?

This makes state the single source of truth. The input shows exactly what is in state, and state always holds the input's latest value.

An uncontrolled input is the opposite: it keeps its own value in the DOM, and you read it when you need it (on submit with FormData, or with a ref).

2. Why does it matter?

UncontrolledControlled
The value lives in the DOMThe value lives in React state
You only learn the value on submitThe value is available on every keystroke
Live validation is hardYou can show errors while the user types
Good enough for simple formsGood for dynamic forms and instant feedback

3. Where is it used?

Use caseWhy controlled
Signup formShow password strength live
Search boxFilter the list on every letter
Character counterA tweet-style limit counter
Dependent fieldsWhen the country changes, the city list changes
Submit buttonDisable it when the form is not valid

4. How does it work?

The controlled input loop
The value is in state
The input takes its value from state
The user types and onChange runs
The handler sets the new value with setState
Re-render, and the input shows the new value

Use value for a text input and checked for a checkbox. Both need onChange.

5. Code example

Controlled text and checkbox

jsx
import { useState } from "react";

export default function SignupForm() {
  const [email, setEmail] = useState("");
  const [agree, setAgree] = useState(false);

  function handleSubmit(e) {
    e.preventDefault();
    console.log({ email, agree });
  }

  const canSubmit = email.includes("@") && agree;

  return (
    <form onSubmit={handleSubmit}>
      <input
        type="email"
        value={email}
        onChange={(e) => setEmail(e.target.value)}
        placeholder="Email"
      />
      <label>
        <input
          type="checkbox"
          checked={agree}
          onChange={(e) => setAgree(e.target.checked)}
        />
        I agree to the terms
      </label>
      <button disabled={!canSubmit}>Signup</button>
    </form>
  );
}

Many fields, one state object

jsx
const [form, setForm] = useState({ name: "", city: "Lahore" });

function handleChange(e) {
  setForm({ ...form, [e.target.name]: e.target.value });
}

<input name="name" value={form.name} onChange={handleChange} />
<select name="city" value={form.city} onChange={handleChange}>
  <option>Lahore</option>
  <option>Karachi</option>
</select>

How does the code work?

PartMeaning
value={email}The input's value comes from state
e.target.valueThe text the user typed
e.target.checkedA boolean for checkboxes
e.preventDefault()Stops the page from reloading
[e.target.name]Updates the right key using the field's name
canSubmitA derived value, not a separate state

React 19: form action

In React 19 you can also write <form action={fn}>. The function receives the FormData, and uncontrolled fields reset on their own after submit. This works better with pending state and server actions. The full details are in Actions, useActionState & useOptimistic.

6. Common mistakes

Mistake: Giving value but not onChange

Why? The input becomes read-only. The user types but nothing changes, and a warning appears in the console.

Better: Always pass onChange along with value, or use defaultValue if you only need an initial value.

Mistake: Starting with undefined state and giving it a string later

Why? The input switches from uncontrolled to controlled, and React shows a warning.

Better: Always start with useState('') for text and false for checkboxes.

Mistake: Reading e.target.value for a checkbox

Why? A checkbox has a value attribute, which is not its checked status.

Better: Use the checked prop and e.target.checked for checkboxes.

Mistake: Forgetting preventDefault in onSubmit

Why? The browser reloads the whole page and the state is lost.

Better: Write e.preventDefault() on the first line of the onSubmit handler, or use a React 19 form action.

7. What to learn before and after

Final quick revision

PointDetail
Controlledvalue + onChange, state is the source of truth
UncontrolleddefaultValue, the DOM holds the value
Checkboxchecked and e.target.checked
SubmitonSubmit + preventDefault
React 19<form action> with FormData