store.ts
export const store = configureStore({
reducer: {
auth: authReducer
},
middleware: [],
});
export type AppDispatch = typeof store.dispatch;
export type RootState = ReturnType<typeof store.getState>;
hooks.ts
export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
authSlice.ts(导致问题的函数)
export const fetchUser = createAsyncThunk(
'users/fetchByTok',
async () => {
const res = await getUser();
return res.data;
}
)
Auth.ts
const Auth = ({ component, isLogged }: {component: any, isLogged: boolean}) => {
const dispatch = useAppDispatch();
useEffect(() => {
dispatch(fetchUser()) // <----------- ERROR
}, []);
return isLogged ? component : <Navigate to='/sign-in' replace={true} />;
}
export default Auth;
我有一个 createAsyncThunk 函数来获取用户,但我实际上无法将其放入dispatch()...
- “AsyncThunkAction”类型的参数不可分配给“AnyAction”类型的参数。
- 类型“AsyncThunkAction”中缺少属性“type”,但类型“AnyAction”中需要属性“type”。ts(2345)
第一次使用这个,所以一个很好的解释会很好:)。
我遇到了同样的问题,对我来说,只需添加即可解决AppDispatch
的类型useDispatch
hook;
const dispatch = useDispatch<AppDispatch>();
useEffect(() => {
dispatch(getUsers());
}, []);
getUsers() 是我的createAsyncThunk
功能
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)