中间件就是非业务的技术类组件。它介于底层逻辑与业务之间,相当于中介的作用。

  1. const logger = (store) => (next) => (action) => {
  2. console.log('dispatch--->', action)
  3. let result = next(action) //加载下一个中间件
  4. console.log('next state--->', store.getState())
  5. return result
  6. }
  7. const throwError = (store) => (next) => (action) => {
  8. try {
  9. next(action)
  10. } catch (e) {
  11. console.log('error--->', e)
  12. }
  13. }
  14. const Store = createStore(reducer, {}, applyMiddleware(logger, throwError))

redux-logger

记录 action 动作并在控制台打印

redux-thunk

处理异步的中间件

redux-sage

Effect 里面包含描述副作用的信息,可以通过 yield 传递厨 sagaMiddleware 执行