Smarten up your ngrx reducers

David Palita
WhozApp
Published in
1 min readAug 3, 2018

Despite ngrx reducers being pure functions, you can make them benefit from angular dependency injection.

By themselves they cannot (since they are just functions, and DI occurs in @Injectable() classes and provider definitions), but ngrx allows for injection tokens in your store definitions.

This means you can use a fully managed class as a factory for your reducer function.

And then provide this as the reducer in your module (feature module here)

The Injectable class is not absolutely necessary (you could directly use the UsefulService in the deps of your token). But using this second indirection brings in a lot of power and organisation since you can inherit from a common abstract BaseReducer, easily split your actions handling in separate methods, etc…

--

--