redux-saga 无法捕获 store 增强器调度的动作

IT技术 reactjs redux react-redux redux-saga
2021-05-17 15:38:57

我需要在我的 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 在增强器之前运行。有什么办法可以让它工作,所以传奇也会从增强器那里捕捉到一个动作吗?

2个回答

试试这个

function* log(action) {
  while (true) {
    yield take('*');
    console.log(action);
  }
}

export default function* root() {
  yield all([call(log)]);
}

只是为了看看它是否有效然后你可以尝试移动到 takeEvery,我确实遇到了问题并且创建了特定于 saga 的句柄工作

解决方案基于applyMiddlware

import createSagaMiddleware from 'redux-saga';
import { takeEvery } from 'redux-saga/effects';
import { createStore, compose } from 'redux';
import {
  reducer
} from '../reducers';

function sagaPreinitMiddleware(saga) {
  return (createStore) => (reducer, preloadedState, enhancer) => {
    const sagaMiddleware = createSagaMiddleware();

    const store = createStore(reducer, preloadedState, enhancer);
    let dispatch = store.dispatch;
    const middlewareAPI = {
      getState: store.getState,
      dispatch: (action) => dispatch(action)
    };
    dispatch = compose(sagaMiddleware(middlewareAPI))(store.dispatch);
    sagaMiddleware.run(saga); // run the saga
    return {
      ...store,
      dispatch
    };
  };
}

const reactReduxFirebase = (next) => {
  return (reducer, initialState, middleware) => {
    const store = next(reducer, initialState, middleware);
    store.dispatch({
      type: 'DUMMY_ACTION'
    });
    setTimeout(store.dispatch({
      type: 'DUMMY_ACTION_1'
    }), 100);
    return store;
  };
};

// usage
const middleware = [];
const store = createStore(
  reducer,
  0,
  compose(
    reactReduxFirebase,
    sagaPreinitMiddleware(sagas)
  )
);


// sagas.js
function* handle(action) {
  console.log(action);
}

function* sagas() {
  yield takeEvery('*', handle);
}

日志:

   00000000: [reducer] action Object {type: "@@redux/INIT"}
   00000008: [reducer] action Object {type: "DUMMY_ACTION"}
             Object {type: "DUMMY_ACTION"}
   00000011: [reducer] action Object {type: "DUMMY_ACTION_1"}
             Object {type: "DUMMY_ACTION_1"}