1
votes

So, I'm checking out redux codebase,

To create a store, we're calling the createStore function exposed by redux,

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)
  } ...

In createStore function above, enhancer is called in this part, which is also the applyMiddleware function that I passed on the first line...

enhancer(createStore)(reducer, preloadedState)

But when I checked the applyMiddleware function, there are 3 parameters (reducer, preloadedState, enhancer)...

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

...

It's basically always undefined right? I'm just curious why it's there or if I'm missing something.

enhancer(createStore)(reducer, preloadedState)
1

1 Answers

2
votes

Yeah, this one comes up (surprisingly) frequently. It's a holdover from the "old-style" way of using createStore. See Redux issue #2128 for discussion and history as to why that argument exists.