-
Notifications
You must be signed in to change notification settings - Fork 321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot dispatch in the middle of a dispatch #71
Comments
If you can, move the action out of the store listener's path. I've seen this happen with transitionTo often, it's probably not a good idea to transitionTo immediately in a store listener. Try setting the transitionTo in a setTimeout/setImmediate so it happens on next tick. |
I've dealt with similar problems using Flux & react-router together. Building off of this document React Router in a Flux App, and ideas similar to @goatslacker I put together a scheme like this gist It gives me a consistent interface for invoking the router, and removes the need to think about when a You'll also see in that gist a mixin I use for sending optional actions on the |
Sorry guys but I think that dispatcher should query and fire in turn actions if they are fired simultaneously. Am I right? If so could you please improve dispatcher of alt. Thanks. |
The dispatcher handles actions synchronously. If an action is fired inside another action's context, the dispatcher will throw an error. As others have said before, if you cannot move the nested action elsewhere, use setTimeout to fire the nested action in a new context. FYI, there is no need to open new issues (#136) just to bump another. |
Guys from facebook say that this solution not bad: https://monosnap.com/image/VfYSlclKucvgSkudGJnliEkn19upZu P.S. setTimeout is not silver bulletin. I can't use setTimeout elsewhere |
By the way sorry for opening new issue |
Apologies, I thought you were the OP of this issue. In your scenario, an action queue would be a good approach. But that would not be part of the dispatcher, the dispatcher will always handle one action at a time. It could possibly be made into a util for alt though. Like fisherwebdev wrote, before firing the actions for each query result, check the dispatcher's |
:) I think in this case code will be a little unreadable. |
Do you have anything in mind in terms of implementation details? My opinion on the matter: in lieu of implementing a custom dispatcher I defer over to Facebook's dispatcher, if they implement a queue then Alt will have it. |
Yep, in flux i've made such dispatcher: |
Cool. You can also effectively do the same thing with |
Your solution is not always suitable because in some cases I need this action to be deffered in some cases I don't need. That's why i've made processing of such situations centralized in dispatcher. |
Well right, you don't always use defer. My example is not intended to be used for each action call, you just need it if you call an action inside of another action (meaning the dispatcher is dispatching already). |
And exactly this situation is complex to catch :). One action can be triggered from view1 another from view2. |
👍 |
@deser Thanks for sharing! Small FYI, it looks like in newer versions of the Flux dispatcher, you can now just call |
Thanks, the setTimeout worked for transitions. |
So is usage of setTimeout OK? Or maybe is it wrong "Flux" way when i do "login" fetch action to do login, then get success result, change login store, and then react component calls action to go to next screen? |
@aksonov setTimeout (or deferring) is generally not desirable but sometimes unavoidable. I follow the "try not to use it" approach. ;) |
So what is correct way for typical login scenario? Pavel.
|
@aksonov this is one of the times I use defer/setTimeout. |
What would you recommend to do in my case: How could i avoid deferring in that case? Sorry if such a question is inappropriate here. |
On photouploaderstore listen to that higher level action that mounts all the things and then clear the store then. Actions can do various things they don't have to be 1:1. |
@goatslacker thanks Josh. |
What's an appropriate timeout to set, 50ms? 100ms? I imagine it must get affected by system resources? |
@jamesfzhang You can set it with 0ms. All you want is for it to be in the next event loop. |
Thanks @jdlehman |
@goatslacker What if I want to make a chain of actions, in a waterfall way?
|
@mib32 I'm not exactly sure what your case is, please clarify. |
From a Login react component, I have a login action that makes an API request and on successful response, it updates a SessionStore. The Login component sees the value of the session store updated through it's props passed down from the parent component, and in shouldComponentUpdate, it does a transitionTo (using react-router) to an account page. The account page then calls an action to update the SessionStore in ComponentWillMount.
I'm getting this error message:
Uncaught Error: Invariant Violation: Dispatch.dispatch(...): Cannot dispatch in the middle of a dispatch.
I believe it's due to the account page trying to update the SessionStore as the result of the SessionStore just getting updated from a previous action. Any suggestions on how to architect this differently?
NOTE: things work when using react-router's HashLocation, but not with the HistoryLocation.
The text was updated successfully, but these errors were encountered: