1

这是源代码。

import {applyMiddleware, createStore} from 'redux'

const reducer = (state = initialState, action) => {
   ...
   if(action.type === 'E'){
      throw new Error("AAAAAAA")
   }
   return initialState;
}

const error = (store) => (next) => (action) => {
   try {
      next(action)
   } catch(e) {
      console.log("BBBBBB", e);
   }
}

const middleware = applyMiddleware(error);

const store = createStore(reducer, 1, middleware);

store.subscribe(() => {
    ...
})

store.dispatch({type: "E"})

结果是BBBBBB Error:AAAAAAA

我认为 redux 中间件位于storestore.dispatch()之间。reducer

当我们向 store 发送一些动作时,store 的 reducer 在中间件函数之后被调用next()

我错了吗?

e但是那个错误中间件怎么能捕获throw new Error("AAAAAAA")到reducer函数中呢?

您可以在https://www.youtube.com/watch?v=DJ8fR0mZM44中看到上述行为

在视频中,讲师说“实际错误从未触发”,但作为记录错误对象的中间件,未能正确理解。

4

0 回答 0