15

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 ?

norbitrial
  • 14,716
  • 7
  • 32
  • 59
Nevosis
  • 1,366
  • 1
  • 16
  • 27

1 Answers1

31

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.

markerikson
  • 63,178
  • 10
  • 141
  • 157
  • Do you if there's a good way to write a test to check if middlewares are being applied in the correct order? Do you think it's beneficial to even do that in the first place? – Imran Ariffin Jan 18 '20 at 03:18
  • I can't immediately think of one, and no, I don't think that's worth testing. That's simply going to be a matter of a couple lines of code in your app setup logic. – markerikson Jan 18 '20 at 16:58