Skip to main content

Changing state is optional

For both sync and async reducers, returning a new state is optional. If you don't plan on changing the state, simply return null. This is the same as returning the state unchanged:

class GetAmountAction extends ReduxAction<AppState> {

Future<AppState?> reduce() async {
int amount = await getAmount();

if (amount == 0)
return null;
else
return state.copy(counter: state.counter + amount));
}
}

This is also useful if some action is used to simply start other async processes, or dispatch other actions. For example:

class InitAction extends ReduxAction<AppState> {

AppState? reduce() {
dispatch(ReadDatabaseAction());
dispatch(StartTimersAction());
dispatch(TurnOnListenersAction());

return null;
}
}

Note the reduce() method has direct access to dispatch.


Next, let's see how to reduce boilerplate by defining our own base action class.