-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
store.dispatch
does not work in 1.2.2
#321
Comments
Could you please hover over Also, are you using custom middlewares? |
* correctly infer dispatch from provided middlewares * simplify getDefaultMiddleware typings * test readability * documentation, update api report * extract tests for >=3.4 * update documentation * Tweak TS usage wording Co-authored-by: Mark Erikson <mark@isquaredsoftware.com>
One more instance of error for ya:
typeof store.dispatch:
This happens to be complaining about types being picked up from 'connected-react-router' but I don't think that's the issue. Edit: Sorry for lack of details, will try to test out the proposed change this evening. |
Hm. As for your comment: It's definitely a How does your |
I am using only thunk middleware:
|
@yhnavein I'll look into that. |
If I delete line with
|
This seems to somehow be triggered by your |
Same problem updating to latest, so here's a code snippet The import { configureStore, Action } from '@reduxjs/toolkit';
import { ThunkAction } from 'redux-thunk';
import { authReducer } from './features/auth/auth.redux';
export const store = configureStore({
reducer: {
auth: authReducer,
},
});
export type AppThunk = ThunkAction<void, RootState, null, Action<string>>;
export type RootState = ReturnType<typeof store.getState>; The thunk in export const runLogoutThunk = (deps?: { tokenStore?: ITokenStore }): AppThunk => async dispatch => {
try {
const { tokenStore = getTokenStore() } = deps || {};
const token = tokenStore.getToken();
tokenStore.removeToken();
if (token) {
const authResult = await authApi.logout(token);
}
} catch (e) {
} finally {
dispatch(getAuthLogout());
}
}; And the TS error import { store } from '../../store';
export const createDefaultApiService = () => {
const serverUrl = 'http://localhost:3000';
const tokenStore = getTokenStore();
const refreshTokenService = new RefreshTokenService({
tokenStore: tokenStore,
refreshTokenUrl: `${serverUrl}/auth/refresh-token`,
});
return new ApiService({
serverUrl,
refreshTokenService: refreshTokenService,
tokenStore: tokenStore,
onAuthFailure: (response): Response => {
/// TYPESCRIPT IS COMPLAINING HERE ABOUT
/// ------------------------------------------------------------------
// error TS2769: No overload matches this call.
// Overload 1 of 2, '(action: AnyAction): AnyAction', gave the following error.
// ...
store.dispatch(runLogoutThunk());
return response;
},
});
}; The complete error
Don't know if it helps getting an idea... I'll try to investigate in the week-end if not found before. Any help appreciated and keep up the fantastic work ! Edit: reverting to 1.2.1 works |
@belgattitude I think I can see the problem there. Does it work if you change it like this? - export type AppThunk = ThunkAction<void, RootState, null, Action<string>>;
+ export type AppThunk = ThunkAction<void, RootState, undefined, Action<string>>; I believe I may have found the root cause for this: Now, we get Dispatch from the middleware via M extends Middleware<infer DispatchExt, any, any>
? DispatchExt extends Function
? DispatchExt
: never
: never and our Middleware is per default
type DefaultMiddleware<S> = ThunkMiddleware<S>
type DefaultMiddleware<S> = ThunkMiddleware<S, AnyAction, undefined, ThunkDispatch<S, undefined, AnyAction>>
type DefaultMiddleware<S> = Middleware<ThunkDispatch<S, undefined, AnyAction>, S, <ThunkDispatch<S, undefined, AnyAction> So we'd extract a Dispatch type of Back in 1.2.1, the Dispatch type was Let's see if my fix above would fix it for @belgattitude - as a result, I might have to specify the ThunkMiddleware to be a |
Both would probably be good, I guess. |
+ export type AppThunk = ThunkAction<void, RootState, undefined, Action<string>>; Nope
Setting it to |
Nice, you went from
@ yhnavein and @jonjaques does that solve your problems as well? |
@phryneas For me it does indeed fix the issue and everything works fine :) |
Okay given that feedback so far, I opened a PR for a fix. Could you please try the CodeSandbox build of that PR with your original code and report if it fixes the problems?
|
Updated to Thanks for your help, very clear and super pro... Amazing :) |
* fix #321 * better type for AppThunk in advanced tutorial * keep backwards-compatiblity with ThunkActions with a `null` ExtraArgument * small tweak, add type test * lint
After migrating to 1.2.2
store.dispatch
stopped working with the following error message:Dispathing through dispatcher from hook works fine though:
I can't use React Hooks in the axios interceptor, so I was using this:
The text was updated successfully, but these errors were encountered: