What's the difference between dispatch and next in Redux middleware?

后端 未结 2 928
情书的邮戳
情书的邮戳 2020-12-23 21:18

What\'s the difference between dispatch and next in Redux middleware?

相关标签:
2条回答
  • 2020-12-23 21:42

    Dispatch initiates new action and it's go through full chain of middlewares.

    Next – send current action into next middleware in chain.

    0 讨论(0)
  • 2020-12-23 21:44
    createStore(reducer,
     applyMiddleware(
     middlewareA,
     middlewareB,
     middlewareC
     )
    );
    

    Calling next(action) within middlewareB will cause the action to be passed to middlewareC and then the reducer. Calling dispatch(action) within middlewareB will cause the action to be passed to middlewareA, then middlewareB, then middlewareC, and finally to the reducer, returning the execution back to middlewareB. Calling dispatch() multiple times is a common and valid practice. next() can also be called more than once, but this is not recommended as any action passed to next() will skip the middleware before the current one (for example, potentially skipping the logging middleware).

    0 讨论(0)
提交回复
热议问题