22

When dispatching an action is the order when it arrives to a reducer and a saga guaranteed?

Can I rely on that it

  1. first enters the reducer
  2. then the saga?

Reducer:

 function reducer(state, action) {

    switch (action.type) {
       case 'MY_ACTION':
       // decorate action so that an epic doesn't have to take data from store
       action.ports = state.itemsModified;                 
       return state;
     }
    }

Saga:

export function* sagaUpdatePorts() {
    yield* ReduxSaga.takeEvery(actions.GRID_PORTS_ASYNC_UPDATE_PORTS, updatePorts);
}

function* updatePorts(action) {
    const {response, error} = yield SagaEffects.call(portsService.updatePorts, action.ports);
}
Amio.io
  • 20,677
  • 15
  • 82
  • 117

1 Answers1

30

Yes. The action hits the reducer first then Sagas.

This is mentioned in the docs under the select() effect API:

It's important to note that when an action is dispatched to the store, the middleware first forwards the action to the reducers and then notifies the Sagas. This means that when you query the Store's State, you get the State after the action has been applied. However, this behavior is only guaranteed if all subsequent middlewares call next(action) synchronously. If any subsequent middleware calls next(action) asynchronously (which is unusual but possible), then the sagas will get the state from before the action is applied. Therefore it is recommended to review the source of each subsequent middleware to ensure it calls next(action) synchronously, or else ensure that redux-saga is the last middleware in the call chain.

romellem
  • 5,792
  • 1
  • 32
  • 64
Yassine Elouafi
  • 6,779
  • 1
  • 20
  • 16