用react-ts中使用redux和redux-thunk遇到无法dispatch异步action的问题
// 某一个组件里
const dispatch = useDispatch()
useEffect(() => {
dispatch(getCodeImg())
// 这一句报错!!!!!!
// 类型“RootThunkAction”的参数不能赋给类型“AnyAction”的参数。
}, [])
// 异步action,thunk中间件
export const getCodeImg = (): RootThunkAction => (dispatch: Dispatch<ActionType>): void => {
post_codeImage() // 一个axios请求
.then(res => {
const { data, code } = res.data
if(code === 200) {
dispatch(actionsCreator(codeImg, data.captchaImg))
dispatch(actionsCreator(key, data.key))
}
})
}
// 一些类型定义
export type ActionType = {
type: string,
value?: unknown,
}
export type RootState = ReturnType<typeof store.getState>
export type RootThunkAction = ThunkAction<void, RootState, unknown, ActionType>
// reducer函数,内容是传统的switch-case就不放了
const loginReducer = (state = inititalState, action: ActionType): LoginState => {}
不明白问题在哪,是TS类型使用的问题还是Thunk在TS中使用的问题,求解!