Is calling another action in store listener an anti-pattern for Flux?

130 Views Asked by At

In my codebase, I fire action(s) inside some store listeners. However, I've heard recently that this is a bad practice. Why is it bad and how should I fix or refactor it? I've done research on this issue but wasn't able to find a good example.

FYI, to fire action(s), I use action.defer provided by Alt.js.

1

There are 1 best solutions below

0
Murilo Cruz On

Your approach is a valid Flux pattern, however when the function that handles actions is firing another action, it easily becomes a bug spot on your code, and it becomes difficult to track the bug.


How to refactor:

Depends on what does the Action currently fired by the store means:

  • If you are firing the action B every time action A is fired, the action creator (the code that calls action.defer) should fire A and then fire B
  • If B is fired just when your store state meets some criteria, you could check for that previousState on the action create, and choose to fire B after A or not.