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
reduce function that is called when reduce(into:action:) is invoked.
Parameters:
reduce: A function that takes aninout Stateand anAction, and returns anEffect<Action>
Type-erase an existing reducer
Reduce instance.
Parameters:
reducer: A reducer that is called whenreduce(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 reducersScope- For embedding child reducers in parent domains@ReducerBuilder- For building complex reducer compositions