happypoulp / redux-tutorial

Learn how to use redux step by step
3.76k stars 546 forks source link

Shouldn't next always be called? #82

Closed egeriis closed 8 years ago

egeriis commented 8 years ago

next is not called here: https://github.com/happypoulp/redux-tutorial/blob/master/09_middleware.js#L68

Does that not prevent the flow to continue?

happypoulp commented 8 years ago

Does that not prevent the flow to continue?

Yes it does prevent the action from reaching the next middleware but it does not prevent the action from reaching redux since we called action(dispatch, getState) and the action is supposed to do the dispatch itself (https://github.com/happypoulp/redux-tutorial/blob/master/09_middleware.js#L117).

The detailled version about why we should not call next(action):

By default redux expect an action to be an object with a type property. This action format is the only one that Redux knows how to handle out-of-the-box.

The role of the thunk-middleware is to handle actions that conform to a specific format: all actions that are functions. If we were to send a function-action to a middleware after the thunk-middleware (by calling next(action)) we would be assuming that after our thunk-middleware, there would be another middleware to handle our function-action. And in our case, there aren't any, thunk-middleware is the only one given to our redux store: https://github.com/happypoulp/redux-tutorial/blob/master/09_middleware.js#L86. This means that if we call next(action), our function-action would finally be given to redux and we would be back to start with this error: https://github.com/happypoulp/redux-tutorial/blob/master/08_dispatch-async-action-2.js#L43

A more concise way to sum up the expected behavior of middleware would be to say that a middleware must:

or

I ended up writing more than I wanted to... Did it make things worse or did you get the idea?