user1885433
user1885433

Reputation:

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

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.

Upvotes: 0

Views: 137

Answers (1)

Murilo Cruz
Murilo Cruz

Reputation: 2521

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.

Upvotes: 0

Related Questions