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:
- LoadDataAction is dispatched
- an
effect
(loadData$) is triggered
- the loadData$-
effect
calls the data-service
- loading the data fails
- the loadData$-
effect
return a LoadDataFailureAction
ngrx processes the action...
- 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)