Redux Toolkit configureStore middleware ordering when using a plain array
0 reputation · 05 Dec 2021, 04:58 UTC
0 reputation · 05 Dec 2021, 04:58 UTC
When using configureStore, developers can supply middleware as either a plain array or a callback that receives getDefaultMiddleware. The documentation notes that passing a plain array does not guarantee the relative order of custom middleware versus the default middleware chain (serializableCheck, immutableCheck, thunk). This creates uncertainty for middleware that depends on side‑effects from those defaults.
Because the default middleware order is not exposed as a public constant and may change between releases, code that assumes a specific sequence can break silently. The lack of a built‑in option to force custom middleware to run before or after the defaults leaves the ordering decision to the user, which remains an unresolved design question.
Does Redux Toolkit provide a way to enforce that custom middleware always executes after the default middleware without using the functional callback? If not, what pattern should be considered a reliable alternative for guaranteeing order?
28775 reputation · 05 Dec 2021, 07:26 UTC
Redux Toolkit does not offer a flag or option that forces custom middleware to run after the default middleware when you supply a plain array to configureStore’s middleware option.
When you pass a plain array, RTK replaces the entire default middleware chain with that array; the defaults are not automatically appended or prepended. Consequently the relative order of your custom middleware versus thunk, immutableCheck and serializableCheck is whatever you place in the array, and there is no built‑in way to guarantee they appear after the defaults.
getDefaultMiddleware() in the order [thunk, immutableCheck, serializableCheck].getDefaultMiddleware.const store = configureStore({
reducer: rootReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat(customLogger, customAnalytics),
});
const store = configureStore({
reducer: rootReducer,
middleware: [...getDefaultMiddleware(), customLogger, customAnalytics],
});
Are you disabling any of the default middleware via options passed to getDefaultMiddleware (e.g., { immutableCheck: false, serializableCheck: false })? If yes, you must mirror those options in the manual list; otherwise the chain will unintentionally omit or duplicate checks.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.