Flux Dispatch.dispatch(…): Cannot dispatch in the middle of a dispatch

后端 未结 4 1716
名媛妹妹
名媛妹妹 2020-12-01 10:52

My code https://gist.github.com/ButuzGOL/707d1605f63eef55e4af

So when I get sign-in success callback I want to make redirect,
redirect works through dispatcher t

相关标签:
4条回答
  • 2020-12-01 11:18

    You can make it work by "scheduling" the next action instead of calling it directly, here is an example code:

    // instead of doing this
    Dispatcher.dispatch(...);
    
    // go like this
    setTimeout(function() {
      Dispatcher.dispatch(...);
    }, 1);
    

    This will cause your next dispatch to be called later out of the current dispatch process, and no error will happen.

    If your dispatch code is on a callback any kind of other async operation that will work as well (for example in a response for an Ajax request).

    I'm using this style to make some forms respond to generic data here and I'm facing no issue, at least the way I'm using it.

    0 讨论(0)
  • 2020-12-01 11:23

    I don't see where in the gist that you posted you are doing the redirect. I only see the AUTH_SIGNIN and AUTH_SIGNIN_SUCCESS actions, and they look pretty straightforward.

    But no, there is no hack to create an action in the middle of a dispatch, and this is by design. Actions are not supposed to be things that cause a change. They are supposed to be like a newspaper that informs the application of a change in the outside world, and then the application responds to that news. The stores cause changes in themselves. Actions just inform them.

    If you have this error, then you need to back up and look at how you're handling the original action. Most often, you can set up your application to respond to the original action, accomplish everything you need to do, and avoid trying to create a second action.

    0 讨论(0)
  • 2020-12-01 11:31

    You can check if the dispatcher is dispatching, such as:

    if(!MyDispatcher.isDispatching()) {
        MyDispatcher.dispatch({...});
    }
    
    0 讨论(0)
  • 2020-12-01 11:36

    you can user the "defer" option in the dispatcher. In your case it would be like:

    Dispatcher.dispatch.defer(...);
    
    0 讨论(0)
提交回复
热议问题