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

Exposing Internal State via Controlled Props

Allowing consumers to control component state externally by accepting value and onChange props.

Exposing Internal State via Controlled Props

What You'll Learn

You'll learn how to let consumers control a component's internal state by exposing value and onChange props, turning your component into a "controlled component."

The Core Idea

When you build a component with internal state, you own that state completely. But sometimes consumers need more control—they might want to validate changes, synchronize multiple components, or manage state externally. Controlled props invert this control: instead of your component managing state internally, it accepts the current state (value) and a callback to request changes (onChange) from its parent.

Think of it like driving a car. An "uncontrolled" component is like cruise control—set it and forget it. A "controlled" component is like manual driving—the parent (driver) maintains full control over speed at all times.

How It Works

Your component becomes stateless regarding the controlled value:

function Input({ value, onChange }) {
  return (
    <input 
      value={value} 
      onChange={(e) => onChange(e.target.value)} 
    />
  );
}

// Parent controls the state
function Parent() {
  const [text, setText] = React.useState('');
  
  return <Input value={text} onChange={setText} />;
}

The Input component doesn't maintain its own state—it merely reflects what the parent provides and reports change requests back up.

Why This Matters

This pattern gives consumers power to:

  • Validate or transform input before accepting changes
  • Coordinate state across multiple components
  • Implement custom logic (debouncing, formatting, etc.)
  • Control initial values and resets

You've surrendered control of when and how state updates, but gained a more flexible, reusable component.

Key Takeaway: Controlled props (value + onChange) let consumers manage component state externally, inverting control from the component to its parent for maximum flexibility.