Course contentsShow
← React
Lesson 1157 of 2,39630. Composition: Inversion of ControlFree lesson

The Controlled vs Uncontrolled Pattern Generalized

Applying the controlled/uncontrolled input pattern to any stateful component for maximum flexibility.

The Controlled vs Uncontrolled Pattern Generalized

What you'll learn: How to apply the controlled/uncontrolled input pattern to any stateful component, not just form inputs.

You've seen controlled vs uncontrolled with <input> elements, but this pattern is actually a fundamental design choice for any component that manages state internally.

The Pattern Beyond Forms

Controlled components don't own their state—the parent passes both the current state value and the handler to change it:

// Controlled accordion
<Accordion 
  openIndex={openIndex} 
  onToggle={setOpenIndex}
>
  {/* sections */}
</Accordion>

Uncontrolled components manage their own state internally, optionally accepting initial values:

// Uncontrolled accordion
<Accordion defaultOpenIndex={0}>
  {/* sections */}
</Accordion>

Why This Matters

This pattern applies to tabs, modals, carousels, pagination, filters—anything with internal state. The choice determines who controls the behavior:

  • Controlled: Parent has full control, can read/modify state, synchronize with other UI
  • Uncontrolled: Component is self-sufficient, simpler API, works standalone

The Hybrid Approach

Many production components support both modes:

function Accordion({ openIndex, defaultOpenIndex, onToggle }) {
  const [internalIndex, setInternalIndex] = useState(defaultOpenIndex);
  
  // Use prop if controlled, internal state if not
  const currentIndex = openIndex ?? internalIndex;
  const handleToggle = onToggle ?? setInternalIndex;
  
  // ...
}

The component checks if control props exist—if yes, use them; if no, fall back to internal state.

Key Takeaway: The controlled/uncontrolled pattern isn't about forms—it's a general strategy for any stateful component to balance flexibility (parent control) with simplicity (self-contained behavior).