Redux 15 - 进阶:异步流(Async Flow)
Without middleware, Redux store only supports synchronous data flow. This is what you get by default with createStore()
.
在不使用中间件的情况下,Redux的Store对象只支持同步数据流,这是使用createStore()
创建Store的默认结果。
You may enhance createStore()
with applyMiddleware()
. It is not required, but it lets you express asynchronous actions in a convenient way.
可以使用applyMiddleware()
方法对createStore()
进行增强。虽然这不是必须的,但使用中间件可以方便的表达异步Action。
Asynchronous middleware like redux-thunk or redux-promise wraps the store’s dispatch()
method and allows you to dispatch something other than actions, for example, functions or Promises. Any middleware you use can then interpret anything you dispatch, and in turn, can pass actions to the next middleware in the chain. For example, a Promise middleware can intercept Promises and dispatch a pair of begin/end actions asynchronously in response to each Promise.
像redux-thunk或redux-promise之类的中间件都是封装了Store对象的dispatch()
方法,以允许你派发Action之外的其他对象,比如函数或者Promise。集成的中间件可以用来解释派发的对应对象,并将Action传递到中间件链中的下一个对象。比如,一个Promise中间件可以解释Promise,并在响应中给各自的Promise派发一对开始/结束的异步Action。
When the last middleware in the chain dispatches an action, it has to be a plain object. This is when the synchronous Redux data flow takes place.
当中间件链中的最后一个对象派发Action时,该Action必须是一个普通Action对象。此时会进入Redux同步数据流中。
Check out the full source code for the async example.
下一步(Next Steps)
Now you saw an example of what middleware can do in Redux, it’s time to learn how it actually works, and how you can create your own. Go on to the next detailed section about Middleware.
之前的代码示例中在Redux中使用了中间件,现在是时候学习一下它是怎么工作的了,并学习如何创建自己的中间件。继续去下一节详细学习中间件吧。