Angular 6 – Why use @ngrx/store rather than service injection

I think you should read those two posts about Ngrx store:

  • Angular Service Layers: Redux, RxJs and Ngrx Store – When to Use a Store And Why?
  • Ngrx Store – An Architecture Guide

If the first one explains the main issues solved by Ngrx Store, it also quote this statement from the React How-To “that seems to apply equally to original Flux, Redux, Ngrx Store or any store solution in general”:

You’ll know when you need Flux. If you aren’t sure if you need it, you don’t need it.

To me Ngrx store solves multiple issues. For example when you have to deal with observables and when responsability for some observable data is shared between different components. In this case store actions and reducer ensure that data modifications will always be performed “the right way”.

It also provides a reliable solution for http requests caching. You will be able to store the requests and their responses, so that you could verify that the request you’re making has not a stored response yet.

The second post is about what made such solutions appear in the React world with Facebook’s unread message counter issue.

Concerning your solution of storing non-obvervable data in services. It works fine when you’re dealing with constant data. But when several components will have to update this data you will probably encounter change detection issues and improper update issues, that you could solve with:

  • observer pattern with private Subject public Observable and next function
  • Ngrx Store

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)