How the middleware in redux uses store as a parameter. For example, if thunk is in store, how can it take store as a parameter?

const thunk = store = > next = > action = >
typeof action = "function"?

action(store.dispatch, store.getState) :
next(action)

let todoApp = combineReducers (reducers) let store = createStore (
todoApp,
applyMiddleware (

thunk,

places))

Jul.18,2022

it is recommended to take a look at the Redux source code:

export default function applyMiddleware(...middlewares) {
  return (createStore) => (reducer, preloadedState, enhancer) => {
    const store = createStore(reducer, preloadedState, enhancer)
    let dispatch = store.dispatch
    let chain = []

    const middlewareAPI = {
      getState: store.getState,
      dispatch: (action) => dispatch(action)
    }
    chain = middlewares.map(middleware => middleware(middlewareAPI))
    dispatch = compose(...chain)(store.dispatch)

    return {
      ...store,
      dispatch
    }
  }
}
The middlewareAPI in the

applyMiddleware is the store generated from the original create Store, that is, the store in the thunk, and the return value of applyMiddleware is the store you finally declared to get, which is different.

Menu