Order of multiple middleware in React Redux

Viewed 5308

What happen if I have multiple middleware (lets say 3 for the exemple), all catching a single action ? Do they trigger in the order defined in the store creation ?

createStore(reducer,applyMiddleware(middle1, middle2, middle3));

middle1 will get triggered first, then middle2, then middle3 ? (when calling next() ) Can I on a specific action force middle3 to be called before middle2 ?

1 Answers

The middleware pipeline exactly matches the order that you passed to applyMiddleware(). So, in that example:

  • calling store.dispatch() passes the action to middle
  • when middle1 calls next(action), it goes to middle2
  • when middle2 calls next(action), it goes to middle3
  • when middle3 calls next(action), it goes to the actual store and the reducer logic is executed

And no, you cannot reorder middleware after the store has been created.

Related