Redux增强器和applyMiddleware

时间:2017-03-17 18:33:00

标签: redux

所以,我正在检查redux代码库,

要创建商店,我们调用redux,

公开的createStore函数
createStore(ourReducer,{}, applyMiddleware(someMiddleware));


export default function createStore(reducer, preloadedState, enhancer) {
  if (typeof preloadedState === 'function' && typeof enhancer === 'undefined') {
    enhancer = preloadedState
    preloadedState = undefined
  }

  if (typeof enhancer !== 'undefined') {
    if (typeof enhancer !== 'function') {
      throw new Error('Expected the enhancer to be a function.')
    }

    return enhancer(createStore)(reducer, preloadedState)
  } ...

在上面的createStore函数中,在这部分中调用了enhancer,它也是我在第一行传递的applyMiddleware函数...

enhancer(createStore)(reducer, preloadedState)

但是当我检查applyMiddleware函数时,有3个参数 (reducer,preloadedState,enhancer)...

export default function applyMiddleware(...middlewares) {
  return (createStore) => (reducer, preloadedState, enhancer) => {

...

它基本上总是未定义的吗?我只是好奇为什么它存在或者我错过了什么。

enhancer(createStore)(reducer, preloadedState)

1 个答案:

答案 0 :(得分:1)

是的,这个经常出现(令人惊讶)。这是使用createStore的“旧式”方式的延续。有关该论证存在的原因,请参阅Redux issue #2128以获取讨论和历史记录。