2016-08-16 16 views
0

我正在学习docs中的reactx中间件,并且想知道他的意思是“该动作实际上会再次穿过整个中间件链? “这是否意味着store.dispatch(action)将具有在中间件末尾返回的最终dispatch,而next(action)将只在中间件功能的某个点处指向dispatchReact Redux中间件的#store.dispatch(动作)对#下一个(动作)

它有点挂羊头卖狗肉,以确保如果你从你的中间件,而不是下一个(动作)调用 store.dispatch(动作), 动作将再次实际行驶的整个中间件链, 包括目前的中间件。正如我们以前所见,这对于异步 中间件非常有用。

+1

可能重复[Redux中间件中dispatch和next之间有什么区别?](http://stackoverflow.com/questions/36160457/whats-the-difference-between-dispatch-and-next-in-redux-中间件) –

+0

见http://jsfiddle.net/ca73kt2v/ – qwertymk

回答

2

在还原middleware是补丁功能dispatch函数来扩展功能。而next功能里面的middleware实际上只是store.dispatch功能的副本。

function patchStoreToAddLogging(store) { 
    let next = store.dispatch 
    store.dispatch = function dispatchAndLog(action) { 
    console.log('dispatching', action) 
    let result = next(action) 
    console.log('next state', store.getState()) 
    return result 
    } 
} 

所以,当你return next(action)你返回一个结果dispatch功能,与以往所有middlewares中应用的链,下一个中间件。

但是,当你调用store.dispatch函数调用从中间件链所有middlewares应用最终dispatch方法的返回。

+0

接下来是未打补丁的调度,它是调用以前调度的新调度[docs](https://github.com/gaearon/redux-thunk) – dmi3y

相关问题