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

Action Types and State Change Objects

Designing a clear action type system that communicates what state changes are happening and why.

Action Types and State Change Objects

What You'll Learn

You'll learn how to design a clear, intention-revealing action type system that makes state changes explicit and debuggable in your state reducer pattern.

The Core Idea

When building a state reducer pattern, every state change should communicate what happened and why it happened through well-named action types. Rather than just passing raw values around, you create action objects that describe the change, and state change objects that represent the proposed new state along with metadata about the change.

Think of it like a ledger in accounting: you don't just write "minus 50"—youwrite"EXPENSERECORDED:OfficeSupplies−50" — you write "EXPENSE_RECORDED: Office Supplies - 50" so anyone can understand what happened and why.

Action Types as Documentation

Action types are constants that describe user interactions or internal events:

const ActionTypes = {
  TOGGLE: 'TOGGLE',
  OPEN: 'OPEN',
  CLOSE: 'CLOSE',
  RESET: 'RESET'
} as const;

These become the vocabulary of your component's state machine.

State Change Objects

Instead of just returning new state, your reducer can return objects that describe the change:

type StateChange = {
  type: typeof ActionTypes.TOGGLE;
  changes: { isOpen: boolean };
};

This allows the consumer's custom reducer to see both the action type and the proposed changes, making it easier to override specific behaviors while preserving others.

Why This Matters

Clear action types make your component's behavior predictable and debuggable. When something goes wrong, you can trace exactly which action fired. When consumers override your reducer, they know precisely what each action means without reading your source code.

Key Takeaway: Well-named action types and structured state change objects transform your reducer from a black box into a self-documenting contract between your component and its consumers.