Course contentsShow
← React
Lesson 453 of 2,39613. Controlled ComponentsFree lesson

The Basic Pattern: useState + value + onChange

Wire state to value and update state in onChange to create a controlled input that stays in sync with React.

The Basic Pattern: useState + value + onChange

What You'll Learn

You'll learn how to wire together React state, the value prop, and the onChange handler to create a fully controlled input that stays perfectly synchronized with your component's state.

The Three-Part Dance

A controlled input in React requires three pieces working together:

  1. useState — Store the input's current value in state
  2. value — Bind the input's displayed value to that state
  3. onChange — Update the state whenever the user types

Think of it like a mirror reflection: the input shows what's in state, and every keystroke updates that state, which updates the display, creating a continuous loop of synchronization.

The Pattern in Code

function NameForm() {
  const [name, setName] = useState('');

  return (
    <input
      value={name}
      onChange={(e) => setName(e.target.value)}
    />
  );
}

Here's what happens:

  • useState('') creates state starting with an empty string
  • value={name} forces the input to display whatever name contains
  • onChange={(e) => setName(e.target.value)} updates name on every keystroke

The input is now "controlled" because React state is the single source of truth. The input can't show anything except what's in state.

Why All Three Matter

Miss one piece and it breaks:

  • No value prop? The input becomes uncontrolled
  • No onChange? The input becomes read-only
  • No state? You have nothing to bind to

Together, they create React's controlled component pattern: state flows down through value, changes flow up through onChange, and everything stays in perfect sync.

Key Takeaway: The controlled input pattern combines useState for storage, value to display state, and onChange to capture updates—creating a single source of truth managed entirely by React.