我需要在我的 redux 应用程序中使用商店增强器(来自react-redux- firebase 的 reactReduxFirebase )。这个增强器调度一个动作,它或多或少看起来像这样(简化了很多):
const reactReduxFirebase = (next) => {
return (reducer, initialState, middleware) => {
const store = next(reducer, initialState, middleware);
store.dispatch({
type: 'DUMMY_ACTION'
});
return store;
}
}
// usage
const sagaMiddleware = createSagaMiddleware();
const middleware = [sagaMiddleware];
const store = createStore(
reducer,
initialState,
compose(
applyMiddleware(...middleware),
reactReduxFirebase
)
);
sagaMiddleware.run(sagas);
// sagas.js
function* handle(action) {
console.log(action);
}
function* saga() {
yield takeEvery('*', handle);
}
export default saga;
我希望 saga 侦听所有操作并在 console.log 中记录它们,但它没有捕获由增强器发送的“DUMMY_ACTION”,因为它是在 saga 开始侦听 ( sagaMiddleware.run(sagas);
)之前发送的。从 redux-saga 文档看来,saga 必须在 applyMiddleware 之后运行,所以我不能让 saga 在增强器之前运行。有什么办法可以让它工作,所以传奇也会从增强器那里捕捉到一个动作吗?