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

Benefits: Conditional Rendering Based on Input

Other components can read the same state to show/hide UI, impossible with DOM-only values.

Benefits: Conditional Rendering Based on Input

You'll learn how controlled components let any part of your app show or hide UI based on what the user types—something impossible with uncontrolled inputs storing values only in the DOM.

The Magic of Shared State

When you store an input's value in React state (making it controlled), that value becomes available to your entire component. You're not locked into just the input field itself—any part of your JSX can read that state and decide what to render.

Think of it like a shared bulletin board: when the input is controlled, you're posting the current value where everyone can see it. When it's uncontrolled, the value is hidden inside the input's private locker (the DOM)—only the input knows what it contains.

Real-World Example

const [username, setUsername] = useState('');

return (
  <div>
    <input 
      value={username} 
      onChange={(e) => setUsername(e.target.value)} 
    />
    
    {username.length > 0 && (
      <p>Hello, {username}!</p>
    )}
    
    {username.length >= 5 && (
      <button>Submit</button>
    )}
    
    {username.length < 3 && username.length > 0 && (
      <span style={{color: 'red'}}>Username too short</span>
    )}
  </div>
);

Here, the greeting, submit button, and error message all appear or disappear instantly as the user types. This works because username lives in state—every component can check its current value during each render.

With an uncontrolled input, you'd need to manually query the DOM to get the value, and you'd have no way to automatically re-render other components when it changes.

Key Takeaway: Controlled components turn input values into reactive data—any component can read that state and conditionally render UI, creating dynamic, responsive interfaces that update automatically as users type.