-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Description
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
Labels
No labels