Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use NGRX instead of constructor injected services?

Wondering why one would use NGRX or NGXS for an Angular app instead of constructor injected services to handle component IO?

Is it only to ensure that component properties references are never mutated without switching out the entire property value reference or is there more to it?

Altnernative to NGRX

per the answer I developed:

Slice.

I believe it does everything NgRx / NgXS does (With the exception of a time machine - but this is easy to implement via delta notifications - already supported). but with zero boilerplate.

Here's an article showcasing some of the capabilities: https://medium.com/@ole.ersoy/storing-users-in-the-reactive-slice-object-store-5ea0fab06256

like image 493
Ole Avatar asked Apr 17 '18 18:04

Ole


People also ask

Why should I use NgRx?

NgRx implements the Flux-Pattern. At a high level, it helps you unify all events and derive a common state in your Angular app. With NgRx, you store a single state and use actions to express state changes. It is ideal for apps with many user interactions and multiple data sources.

What are the benefits of NgRx effects?

It provides several advantages by simplifying your application state to plain objects, enforcing unidirectional data flow, and more. The Ngrx/Effects library allows the application to communicate with the outside world by triggering side effects.

When should I use NgRx data?

Quoting NgRx Data documentation, typical usage of this library is entity data management. Basically it means that if you have complex entity collection you should go hard way and use @ngrx/entity where the boilerplate code is huge.

Which is better NgRx or RxJS?

If you want to take advantage of single direction data flow using Store in Angular NgRx is the solution. Using NgRx store you can create your store, effects , reducers & actions in any angular app. On the other hand RxJS is used for mainly for consuming api data and creating shared services using subject etc.


1 Answers

You will need to write a service that provides a consistent and easy to change api to modify the data, you will need to come up with a fast and well tested way of querying the data, you will need to write and maintain observables for all your data. You will have to write and establish a pattern for async calls. You will have to write an api to access the data in your templates.

By the time you are done you will end up with something resembling ngrx.

For a simple one service data, ngrx is overkill, lots of boilerplate, but for a reactive app with multiple data sources or complex data interactions across numerous developers having a well used library is really helpful.

Follow SO answer for more in depth explanation: What are benefits of using store (ngrx) in angular 2

To understand, when to use which approach, read this: RxJs and Ngrx Store - When to Use a Store And Why?

like image 146
ashfaq.p Avatar answered Oct 20 '22 00:10

ashfaq.p