Multiple Actions dispatched in Effect #4255
-
Hello there! I have a dilemma at work with a colleague about NGRX and actions dispatched in an effect. I suggested that we could have one action that would trigger an effect and a reducer for different objects. Because in that specific case the reducer blocked or unblocked an item, and when the item was blocked it would dissapear from another api we used. I found it reasonable to have one action that would at that point trigger an update in a reducer and then also reload the other api through an effect. My colleague disagrees and want to have one effect handling everything by dispatching two actions such as:
Where we now instead of having one action dispatched elsewhere in the code triggering a separate effect and a separate case in the reducer. Instead have and effect that now dispatches two actions two get the new data from the api and also update the other object in the state. In my understanding this is an anti-pattern and not reccomended. I also saw the rule: no-multiple-actions-in-effects I have tried arguing for using my suggested approach since it seems more aligned with best practices. But I might very well be wrong. So I would love to get some input. What is wrong? Are we both in the wrong? Are there any good explanations and motivations for either or? Anything in the official docs about this? My argument for the approach I suggested is that its easier to follow and more aligned with best practices. But my colleague just says its wrong and he doesn't agree. Not sure how to respond to that. Grateful for any answers :) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
There are always use cases where best practices don't apply. Best Practices aren't absolute. Saying that, if you have two choices, where the "best practices" might not be optimal but there is not much that separates these two, I'd still go with the "best practice". So I wouldn't say your colleague is wrong, it is more about pragmatism and the "broken windows"-effect. |
Beta Was this translation helpful? Give feedback.
-
Hello, I have read and understood the reasoning behind the rule "no-multiple-actions-in-effects", but what if I want to dispatch multiple async actions, each one waiting for the previous one to finish (because they depend on slices of the state affected by each)? In fact, as a general concept, how can I know that the state is not about to change? Should I use booleans such as To use an example similar to the one used in the rule, say my state already contains a company list and an employees list, but I want to refresh it and clean data for all employees whose name is John. So I would like to:
Based on my own suggestion above, I should have booleans like |
Beta Was this translation helpful? Give feedback.
It boils down to how you see actions.
Are these unique events in the application, commands, a bit of both?
In the docs we see/suggest actions as unique events in an application, https://ngrx.io/guide/store/actions#actions
With that in mind, the response of an effect is a unique event (and a single action).
That's the reason why the rule exists.
If the distinction of an action isn't clearly communicated this leads to different opinions and thus also different implementations, such as the discussion of having a single action (event) or multiple actions (commands) dispatched.