Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How does redux-saga watch on actions dispatched in routesMap thunk? #460

Open
yuejiang-code opened this issue May 29, 2020 · 0 comments
Open

Comments

@yuejiang-code
Copy link

Hi, I'm trying to dispatch an action in routesMap thunk, but I'm not sure how to do that since I can't run sagaMiddleware.run(sagas) until I have created a store. And I can't create a store without passing redux-first-router middleware. So I'm in a chicken and egg problem. I was able to get the thunk dispatch to work with a setTimeout dispatch, but that's not working out well as my component depends on the data fetched by routesMap thunk dispatch. Any advice?


const routesMap = {
  HOME: {
    path: "/",
    thunk: (dispatch, state) => {
      setTimeout(() => {
        dispatch({ type: actions.FETCH_CASH_REPORTING_DATA });
      }, 1000);
    },
    component: "CashReportingView",
  },
};

export default routesMap;
import createSagaMiddleware from "redux-saga";
import { composeWithDevTools } from "redux-devtools-extension";
import { createLogger } from "redux-logger";
import { connectRoutes } from "redux-first-router";
import _ from "lodash";

import { reducers, sagas } from "../ducks";
import routesMap from "./routesMap";

const {
  reducer: locationReducer,
  middleware: locationMiddleware,
  enhancer,
} = connectRoutes(routesMap);

const pageReducer = (state = "CashReportingView", action = {}) => {
  return _.get(routesMap, "[action.type].component", state);
};

const reducer = combineReducers({
  page: pageReducer,
  location: locationReducer,
  cashReportingView: reducers.cashReportingReducer,
});

const loggerMiddleware = createLogger();
const sagaMiddleware = createSagaMiddleware();
const middlewares = composeWithDevTools(
  applyMiddleware(sagaMiddleware, locationMiddleware, loggerMiddleware)
);
const enhancers = compose(enhancer, middlewares);

const store = createStore(reducer, enhancers);

sagaMiddleware.run(sagas);
export default store;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant