-->

redux-saga cannot catch actions dispatched by stor

2019-06-24 15:07发布

问题:

I need to use store enhancer (reactReduxFirebase from react-redux-firebase) in my redux app. This enhancer dispatches an action, it looks more or less like this (much simplified):

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;

I want saga to listen to all actions and console.log them, but it doesn't catch 'DUMMY_ACTION' dispatched by enhancer, because it's being dispatched before saga starts to listen (sagaMiddleware.run(sagas);). From redux-saga docs it seems that saga must be run after applyMiddleware, so I cannot make saga run before enhancer. Is there any way to make it work, so the saga will catch an action from enhancer as well?

回答1:

try this for sagas

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

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

just to see if it works then you can try moving to takeEvery, I did have issues with it and creating handles specific to saga worked



回答2:

The solution based on 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);
}

The log:

   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"}