Skip to main content

Reduce

Reduce is a type-erased reducer that invokes a given reduce function. It’s useful for injecting logic into a reducer tree without the overhead of introducing a new type that conforms to Reducer.

Type Signature

Initializers

Initialize with a reduce function

Initializes a reducer with a reduce function that is called when reduce(into:action:) is invoked. Parameters:
  • reduce: A function that takes an inout State and an Action, and returns an Effect<Action>

Type-erase an existing reducer

Type-erases an existing reducer by wrapping it in a Reduce instance. Parameters:
  • reducer: A reducer that is called when reduce(into:action:) is invoked

Usage

Basic usage in a reducer body

Combining with other reducers

Reduce can be composed with other reducer builders:

When to Use

  • Inline logic: When you want to define reducer logic inline without creating a separate conforming type
  • Simple features: For straightforward state management that doesn’t require complex composition
  • Type erasure: When you need to wrap an existing reducer to hide its concrete type

See Also

  • Reducer - The base protocol for all reducers
  • Scope - For embedding child reducers in parent domains
  • @ReducerBuilder - For building complex reducer compositions