Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use ngrx/effect in angular2

I have an anuglar2 project that communicates with an api. Recently, I decided to integrate ngrx/store to maintain the state of the components, and follow the dump-smart component architecture. But then while moving on, I read about ngrx/effect which can be used upon the api requests.

And here my question comes, why should I use the ngrx/effect library, over just calling the corresponding function in my service from my container component to perform the api request and on success dispatch action to save the returned values in my store.

like image 327
Salma Hamed Avatar asked May 22 '17 13:05

Salma Hamed


People also ask

What is the use of NgRx effects?

NgRx Effects are a popular part of NgRx, a state management library built for Angular. Effects are usually used to perform side effects like fetching data using HTTP, WebSockets, writing to browser storage, and more.

When should I use NgRx?

NgRx is a popular solution in the Angular ecosystem if you are building complex web applications with sophisticated state management. Characteristics of the need for NgRx are many user interactions and multiple data sources. NgRx is a good choice, if: the state should be accessed by many components and services.

Does NgRx improve performance?

Luckily, NgRx provides state and action sanitizers that can be used to clean up your data to reduce the amount of state (for example, multiple arrays with thousands of items) to improve the performance of the devtool.

What is the difference between NgRx and Redux?

NgRx, which is the Angular version of Redux for React is for State Management. In React, state management can get complicated, and Redux is there to help. For Angular, this should not be the case as everything is synced due to two way data binding.


1 Answers

If your case stays that simple, then you won't need an effect for it. Besides, the effect itself will do not much more than calling your service-method.

In short: If you project is very small and without many features, it will just cause you to write more code.

If you project is large, it will help you structure your data-flows and project-components.


When to use an effect:

When you want to trigger an action based on another action(in spoken english you would call this a side-effect) or when you want to add a generic error-handling or maybe a logging.

The way an effect works: The effect listens for any defined action(s) (e.g. LoadDataAction), then does some processing and returns any action(s) that are then processed by the store and distributed to a reducer or some other effect.

Example:

  1. LoadDataAction is dispatched
  2. an effect(loadData$) is triggered
  3. the loadData$-effect calls the data-service
  4. loading the data fails
  5. the loadData$-effect return a LoadDataFailureAction

ngrx processes the action...

  1. the LoadDataFailureAction might be picked up by:
    • a logger-effect(e.g. sends message to a server)
    • by an ui-notification-effect(e.g. displays message to user)
    • and/or by a reducer(e.g. persists the error-count or deletes something from the state)
like image 92
olsn Avatar answered Oct 01 '22 20:10

olsn