Controlled Forms: Single Source of Truth Principle
What you'll learn: How controlled components make React state the definitive authority for all form input values.
The Core Idea
In a controlled form, React state holds the single source of truth for every input's value. The input doesn't manage its own value internally (like native HTML inputs do) — instead, React tells the input what to display, and the input reports changes back to React. This creates a one-way data flow where React state is always authoritative.
Think of it like a puppeteer controlling a marionette. The puppet (input) doesn't move on its own — the puppeteer (React state) controls every movement, and strings (props and callbacks) connect them.
Why This Matters
Without this pattern, you'd have two sources of truth: the DOM's internal state AND your React state. When these drift out of sync, bugs emerge. With controlled components, React state is the only truth, and the input is merely a visual reflection of that state.
The Pattern in Action
function ControlledInput() {
const [value, setValue] = useState('');
return (
<input
value={value} // React tells input what to show
onChange={(e) => setValue(e.target.value)} // Input reports changes
/>
);
}
Notice: The value prop forces the input to always display what's in state. Without the onChange handler, the input would be frozen — you couldn't type because React wouldn't update the state.
Single Source Benefits
- Validation before storage: You can reject or transform input before it reaches state
- Predictable state: Always know what's in the input by checking state
- Easy resets: Just update state, input follows automatically
Key Takeaway: In controlled forms, React state owns the value completely — the input is just a controlled view that displays state and reports user changes back to React.