{"record":{"id":"c7e08a43c4f1a507","repo":"reduxjs/redux","slug":"dispatching-while-constructing-your-middleware-is","errorCode":null,"errorMessage":"Dispatching while constructing your middleware is not allowed. Other middleware would not be applied to this dispatch.","messagePattern":"Dispatching while constructing your middleware is not allowed\\. Other middleware would not be applied to this dispatch\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/applyMiddleware.ts","lineNumber":59,"sourceCode":"  middleware4: Middleware<Ext4, S, any>\n): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 & Ext4 }>\nexport default function applyMiddleware<Ext1, Ext2, Ext3, Ext4, Ext5, S>(\n  middleware1: Middleware<Ext1, S, any>,\n  middleware2: Middleware<Ext2, S, any>,\n  middleware3: Middleware<Ext3, S, any>,\n  middleware4: Middleware<Ext4, S, any>,\n  middleware5: Middleware<Ext5, S, any>\n): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 & Ext4 & Ext5 }>\nexport default function applyMiddleware<Ext, S = any>(\n  ...middlewares: Middleware<any, S, any>[]\n): StoreEnhancer<{ dispatch: Ext }>\nexport default function applyMiddleware(\n  ...middlewares: Middleware[]\n): StoreEnhancer<any> {\n  return createStore => (reducer, preloadedState) => {\n    const store = createStore(reducer, preloadedState)\n    let dispatch: Dispatch = () => {\n      throw new Error(\n        'Dispatching while constructing your middleware is not allowed. ' +\n          'Other middleware would not be applied to this dispatch.'\n      )\n    }\n\n    const middlewareAPI: MiddlewareAPI = {\n      getState: store.getState,\n      dispatch: (action, ...args) => dispatch(action, ...args)\n    }\n    const chain = middlewares.map(middleware => middleware(middlewareAPI))\n    dispatch = compose<typeof dispatch>(...chain)(store.dispatch)\n\n    return {\n      ...store,\n      dispatch\n    }\n  }\n}","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/reduxjs/redux/blob/3084fc33bb23ab4cab4796172eafe342da4e73d9/src/applyMiddleware.ts#L41-L77","documentation":"applyMiddleware() assigns a temporary dispatch placeholder that throws whenever a middleware invokes store.dispatch while it is still being constructed. During setup the chain has not yet been composed onto the real store.dispatch, so any dispatch fired in that window would silently bypass all other middleware. Redux refuses to run it rather than emit a partial pipeline.","triggerScenarios":"A middleware function (the first curried arg handed ({ getState, dispatch }) => next => action => ...) calls middlewareAPI.dispatch(...) synchronously inside its outermost factory body, i.e. during middlewares.map(middleware => middleware(middlewareAPI)) at applyMiddleware.ts:69, before dispatch = compose(...chain)(store.dispatch) has assigned the real dispatch.","commonSituations":"Middleware that auto-dispatches an init/seed action when instantiated; a logging/analytics middleware that eagerly reports its own startup; migration from redux-thunk where dispatch is called at module top-level while configuring the store; SSR code that dispatches during store assembly.","solutions":["Move the eager dispatch out of the middleware factory body and into the innermost handler (next => action => { ... }) or a side-effect scheduled after setup (queueMicrotask / a dedicated INIT action dispatched by the app).","If you need an initial action, dispatch it from your application code after createStore(applyMiddleware(...)(...)) returns, not from inside the middleware constructor.","If the dispatch is genuinely meant to run before the store is ready, restructure so the work happens in the next => action => layer where the composed dispatch is available."],"exampleFix":"// before\nconst myMiddleware = ({ dispatch }) => {\n  dispatch({ type: 'MIDDLEWARE_READY' }) // throws: runs during construction\n  return next => action => next(action)\n}\n\n// after\nconst myMiddleware = ({ dispatch }) => {\n  return next => action => {\n    if (action.type === 'APP_INIT') dispatch({ type: 'MIDDLEWARE_READY' })\n    return next(action)\n  }\n}","handlingStrategy":"validation","validationCode":"// No dispatch should run during middleware construction.\n// Inspect each middleware factory body for calls to `dispatch`/`store.dispatch`\n// BEFORE they are wrapped by `next => action => ...`.\nconst assertNoEagerDispatch = mw => {\n  const api = {\n    getState: () => ({}),\n    dispatch: () => { throw new Error('eager dispatch in middleware factory') }\n  }\n  mw(api) // triggers outer factory only; inner next=> is returned, not invoked\n  return true\n}","typeGuard":"const isMiddleware = x => typeof x === 'function'\n// Guard the call site so only well-formed middleware reaches applyMiddleware:\nconst safeMiddlewares = middlewares.filter(isMiddleware)","tryCatchPattern":null,"preventionTips":["Never call api.dispatch() inside the outer middleware factory body; only inside `next => action => ...`.","Review each middleware for any dispatch that is not nested inside the next-handler.","Add a unit test that calls applyMiddleware(...)(createStore)(reducer) with each middleware and asserts no throw."],"tags":["redux","middleware","dispatch","applymiddleware","store-setup"],"backgroundTag":null,"analyzedSha":"3084fc33bb23ab4cab4796172eafe342da4e73d9","analyzedAt":"2026-08-12T13:21:41.727Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}