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

The setState → Re-render → DOM Update Flow

Every keystroke triggers setState, re-render, and React syncs the new value back to the DOM.

The setState → Re-render → DOM Update Flow

What you'll learn: How every keystroke in a controlled input triggers a complete React update cycle.

The Three-Step Dance

When you type in a controlled input, React performs a precise sequence:

  1. setState is called — Your onChange handler captures event.target.value and calls the state setter
  2. Component re-renders — React executes your component function again with the new state value
  3. DOM updates — React compares the virtual DOM and syncs the changed value prop back to the actual input element

Why This Matters

This is why controlled components feel "responsive" — the flow happens so fast it's invisible to users. Type the letter "a":

// Step 1: onChange fires
<input value={text} onChange={(e) => setText(e.target.value)} />
// User types "a" → setText("a") is called

// Step 2: Component re-renders with text = "a"
function MyComponent() {
  const [text, setText] = useState(""); // now returns "a"
  return <input value={text} ... />; // value prop is now "a"
}

// Step 3: React updates the DOM
// The input's value attribute becomes "a"

Each keystroke repeats this entire cycle. This is the "single source of truth" in action — state flows down to the DOM, never the reverse.

The Synchronization Loop

Think of it like a mirror: the input reflects what's in state, and every change bounces back through state before appearing in the DOM. You're never reading directly from the DOM — you're always reading from React state.

Key Takeaway: Every character typed triggers setState → re-render → DOM sync. This complete round-trip ensures React state remains the authoritative source for the input's value.