Redux Toolkit 2.0 createAsyncThunk abort signal: Should loading state reset automatically?
0 reputation · 01 May 2023, 02:15 UTC
0 reputation · 01 May 2023, 02:15 UTC
When using Redux Toolkit 2.0's createAsyncThunk with an AbortSignal, the thrown AbortError is transformed into a rejected action whose error.message is set to "Aborted". The library does not automatically reset loading flags or other UI state tied to the request, leaving it to developers to dispatch a separate cleanup action or to interpret the rejected action's metadata.
This design choice creates variability across codebases: some teams manually clear state in a reducer, others rely on the rejected action, and a few forget to handle aborts altogether. The unresolved question is whether a future version should provide a built‑in helper (e.g., an abortReducer) that automatically clears loading state on abort, or retain the manual approach to preserve flexibility for custom side‑effects and middleware.
Should Redux Toolkit ship an automatic loading‑state reset for aborted thunks? What impact would such a helper have on existing reducers, middleware chains, and upgrade paths? How could the library version the change to avoid breaking current implementations that depend on the current manual handling?
As of Redux Toolkit 2.0, createAsyncThunk with an AbortSignal dispatches a rejected action where error.message is set to Aborted, but the loading state flag is not automatically cleared. The library leaves this responsibility to the developer, either through a manual cleanup reducer or by interpreting the rejected action's metadata.
createAsyncThunk with an AbortSignal that is aborted before the async operation resolves.store.getState() or Redux DevTools.error.message equals Aborted.An automatic reset would intercept rejected actions with error.message=Aborted and mutate UI state, but this could conflict with reducers that intentionally preserve loading context or perform side‑effects on abort. A versioned opt‑in mechanism—such as a dedicated reducer helper or a createAsyncThunk option—would allow gradual migration without forcing changes on existing implementations.
A diagnostic detail that could refine the recommendation: does your middleware or root reducer intercept rejected async actions before they reach your UI loading reducer?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.