下一步如何在自定义react redux中间件中定义

How is next defined in a custom react redux middleware?

本文关键字:redux 中间件 定义 react 自定义 下一步      更新时间:2023-09-26

在中间件过程中,Dan将next定义为原始调度,但在创建中间件时,他没有这样做。next在这里定义的位置和方式:

const logger = store => next => action => {
  console.group(action.type)
  console.info('dispatching', action)
  let result = next(action)
  console.log('next state', store.getState())
  console.groupEnd(action.type)
  return result
}

Where

nextapplyMiddleware供给

从引用的源,

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

middlewareAPI是传递给中间件的第一级参数。按惯例命名为store

store.dispatch是提供的next,它是第二级参数。