I know that this question has been asked more than once in different ways, but I have not yet found the “right” answer (maybe it just isn't), so I'm looking for the “most thread”.
A simple example:
Options:
one.
LoginForm._onSubmit() calls LoginAction.login()LoginAction.login() calls API.login() with callbacks / promises, and then upon successful login, it calls InfoAction.requestInfo()
2.
LoginForm._onSubmit() calls API.login()- If
API.login() is successful, it calls LoginAction.loginSuccess() and:- or
InfoAction.requestInfo() , which calls API.requestInfo() - or
API.requestInfo() , which then calls InfoAction.infoSuccess()
3.
LoginForm._onSubmit() calls LoginAction.login()InfoStore listens for the LOGIN_OK action and calls API.requestInfo()API.requestInfo() calls InfoAction.infoSuccess() and dispatches an INFO_OK event with a payload of specific information that will be stored in InfoStore
(4.)
calling an API / ServiceProvider or ActionCreators from componentWillMount or componentDidMount seems initially bad. Not really a (good) option, but I put it here for the sake of completeness.
My mark:
1. Good in the "old style" JS-based callback / promise, but doesn't seem to be a Flux way, because we should avoid chaning actions. Just fire and forget.
2. It violates the “flow diagram” a bit - the components speak directly to the API or ServiceProviders, and not to ActionCreators. I'm not sure if this is good or bad. It seems to be “one-way” (good) and avoiding circular need (good). I personally prefer this option (in particular 2.2.)
3. I personally avoid this approach because it will mean that the Store is talking to an API / ServiceProvider that breaks the “flow diagram”, but again, I don’t know if this is really bad (maybe I'm just not used to to doing Flux stuff) Even @fisherwebdev seems to be okay with that (e.g. https://stackoverflow.com/a/16677/ ... ), but is this really the best approach?
4. Bad, bad, bad!
Question
Which one is the "best" and / or is there any other "very thread" for this?
javascript asynchronous reactjs reactjs-flux flux
TooManyQuestions
source share