Skip to content

AsyncThunkAction is not assignable to parameter of type AnyAction in RTK middleware #1882

@fultonm

Description

@fultonm

When dispatching an async thunk action from custom Middleware. A simple MVE is to drop in the custom middleware code below into the redux-typescript CRA, and update the middleware registration in store.ts configureStore to have middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(customMiddleware)

The only version of Redux in use is 4.1.2, and RTK is at 1.7.1

It seems the dispatch function available to us on the store from RTK's Middleware is not capable of dispatching AppThunk or AsyncThunkAction like useAppDispatch()? How can we get access to useAppDispatch() behavior in a middleware?

import { Middleware } from '@reduxjs/toolkit';
import { incrementAsync } from './counter/counterSlice';

const customMiddleware: Middleware = store => {
 
  return next => action => { 
    store.dispatch(incrementAsync(1)) 
    next(action);
  }
}
 
export default customMiddleware;

Here it is on codesandbox for convenience:
https://codesandbox.io/s/aged-mountain-ho3j9?file=/src/features/customMiddleware.ts

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions