TypedReducer< State, Action> class
A convenience class for binding Reducers to Actions of a given Type. This allows for type safe Reducers and reduces boilerplate.
Example
In order to see what this utility function does, let's take a look at a regular example of using reducers based on the Type of an action.
// We define out State and Action classes.
class AppState {
final List<Item> items;
AppState(this.items);
}
class LoadItemsAction {}
class UpdateItemsAction {}
class AddItemAction{}
class RemoveItemAction {}
class ShuffleItemsAction {}
class ReverseItemsAction {}
class ItemsLoadedAction<Item> {
final List<Item> items;
ItemsLoadedAction(this.items);
}
// Then we define our reducer. Since we handle different actions in our
// reducer, we need to determine what kind of action we're working with
// using if statements, and then run some computation in response.
//
// This isn't a big deal if we have relatively few cases to handle, but your
// reducer function can quickly grow large and take on too many
// responsibilities as demonstrated here with pseudo-code.
final appReducer = (AppState state, action) {
if (action is ItemsLoadedAction) {
return new AppState(action.items);
} else if (action is UpdateItemsAction) {
return ...;
} else if (action is AddItemAction) {
return ...;
} else if (action is RemoveItemAction) {
return ...;
} else if (action is ShuffleItemsAction) {
return ...;
} else if (action is ReverseItemsAction) {
return ...;
} else {
return state;
}
};
What would be nice would be to break our big reducer up into smaller
reducers. It would also be nice to bind specific Types of Actions to
specific reducers so we can ensure type safety for our reducers while
avoiding large trees of if
statements.
// First, we'll break out all of our individual State Changes into
// individual reducers. These can be easily tested or composed!
final loadItemsReducer = (AppState state, LoadTodosAction action) =>
return new AppState(action.items);
final updateItemsReducer = (AppState state, UpdateItemsAction action) {
return ...;
}
final addItemReducer = (AppState state, AddItemAction action) {
return ...;
}
final removeItemReducer = (AppState state, RemoveItemAction action) {
return ...;
}
final shuffleItemsReducer = (AppState state, ShuffleItemAction action) {
return ...;
}
final reverseItemsReducer = (AppState state, ReverseItemAction action) {
return ...;
}
// We will then wire up specific types of actions to our reducer functions
// above. This will return a new Reducer<AppState> which puts everything
// together!.
final Reducer<AppState> appReducer = combineReducers([
new TypedReducer<AppState, LoadTodosAction>(loadItemsReducer),
new TypedReducer<AppState, UpdateItemsAction>(updateItemsReducer),
new TypedReducer<AppState, AddItemAction>(addItemReducer),
new TypedReducer<AppState, RemoveItemAction>(removeItemReducer),
new TypedReducer<AppState, ShuffleItemAction>(shuffleItemsReducer),
new TypedReducer<AppState, ReverseItemAction>(reverseItemsReducer),
]);
- Implemented types
-
- ReducerClass<
State>
- ReducerClass<
Constructors
- TypedReducer(State reducer(State state, Action action))
-
Creates a reducer that will only be executed if the dispatched action
matches the
Action
type.
Properties
Methods
-
call(
State state, dynamic action) → State -
The Reducer function that converts the current state and action into a
new state
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited