Course contentsShow
← React
Lesson 1196 of 2,39631. Forms in ReactFree lesson

Uncontrolled Forms: Letting the DOM Manage State

Exploring uncontrolled components where DOM nodes hold their own state, accessed via refs when needed.

Uncontrolled Forms: Letting the DOM Manage State

What you'll learn: How to build forms where input elements maintain their own internal state, just like native HTML, and access values only when needed using refs.

The Opposite Approach to Form Management

After learning controlled forms (where React state drives every keystroke), uncontrolled components flip that model: the DOM itself holds the current value, not your React state. You only read the value when you actually need it—typically on form submission.

Think of it like a vending machine. With controlled forms, you watch every coin drop and track the total in real-time. With uncontrolled forms, you just check the coin box when someone presses "purchase"—you trust the machine to track its own contents until that moment.

How It Works

Instead of value and onChange, you use a ref to grab the DOM node directly:

function UncontrolledForm() {
  const nameRef = useRef(null);

  function handleSubmit(e) {
    e.preventDefault();
    // Read the value only when needed
    console.log(nameRef.current.value);
  }

  return (
    <form onSubmit={handleSubmit}>
      <input ref={nameRef} defaultValue="Initial value" />
      <button type="submit">Submit</button>
    </form>
  );
}

Notice defaultValue instead of value—you're setting an initial state, then letting the input manage itself. No onChange handlers cluttering your component. The DOM tracks the user's typing; you just peek at it via nameRef.current.value when submitting.

When This Makes Sense

Uncontrolled forms shine for simple forms where you don't need real-time validation or derived state. Less code, less complexity, closer to vanilla HTML behavior.

Key Takeaway: Uncontrolled components delegate state management to the DOM itself, using refs to extract values only when necessary—simpler code for forms that don't need per-keystroke reactivity.