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

What the State Reducer Pattern Solves

Understanding how state reducer gives consumers control over internal state transitions in reusable components.

What the State Reducer Pattern Solves

You'll learn why reusable components sometimes need to give consumers control over their internal state transitions.

The Problem: One Size Doesn't Fit All

You've built a beautiful, reusable component with sophisticated internal state logic. It works perfectly... until a consumer needs slightly different behavior in one specific case.

Maybe your <Dropdown> auto-closes on selection, but one user needs it to stay open for multi-select. Your <Toggle> switches freely, but another app needs to block certain state changes based on permissions.

You could add props for every edge case (closeOnSelect, allowToggleWhen, etc.), but this approach quickly becomes unmaintainable. You can't predict every variation users will need.

The Solution: Give Control Over State Changes

The State Reducer pattern inverts control of how state transitions happen. Instead of the component dictating "when you click, the state becomes X," it says: "when you click, you decide what the state becomes."

Think of it like editing a document with "track changes." The document proposes edits, but you review and approve/modify each one before it takes effect.

The component still manages state internally and suggests transitions, but consumers provide a "reducer" function that intercepts every state change and can:

  • Approve it as-is
  • Modify it
  • Block it entirely
  • Add custom logic based on their app's needs

This pattern gives consumers surgical control without requiring the library author to expose dozens of configuration props.

Key Takeaway: The State Reducer pattern solves the "I need custom behavior in this one case" problem by letting consumers intercept and customize internal state transitions, providing maximum flexibility without bloating the component's API.