Search code examples
reactjsreact-routerreact-router-reduxredux-sagareact-router-v4

Access to store.dispatch in a saga for use with react router redux


I'm currently refactoring a react + redux + saga + react-router 3 application to use the new react-router 4 due to breaking changes. Before I would use browserHistory to direct to an appropriate path based on the results from a saga. Due to react-router 4 changes, I can't use browserHistory any longer.

Now I've incorporated react-router-redux to essentially do what browserHistory did. The problem is that react-router-redux only works within a store.dispatch, e.g. store.dispatch(push('/')). I can't seem to find a way to access either the store or it's dispatch function inside my sagas. Any ideas on how to access store.dispatch within a saga? I know you can pass arguments in the root saga but I don't know how to retrieve them in my actual sagas.


Solution

  • Use redux-saga's put effect, which dispatches redux actions to the store - docs.

    import { call, put } from 'redux-saga/effects'
    // ...
    
    function* fetchProducts() {
      const products = yield call(Api.fetch, '/products')
      // create and yield a dispatch Effect
      yield put({ type: 'PRODUCTS_RECEIVED', products })
    }