我有一个Thunk Action,看起来像:
export const doTestCall = (teamId:string): ThunkAction<()=>void, RootState, null, MoodAction> => {
return dispatch => {
const unsubscribe = db.doc("id").onSnapshot(doc =>{
//whatever
});
return unsubscribe;
}
}
在我的react组件中,我有以下钩子:
useEffect(() => {
const unsubscribe = dispatch(doSomething()) as ()=>void;
return () => unsubscribe();
}, []);
这段代码确实有效。我有一个问题,我如何才能取消强制
as ()=>void
如果我这样做,代码会在";返回线路";带有:
const unsubscribe: (dispatch: ThunkDispatch<RootState, null, MoodAction>, getState: () => RootState, extraArgument: null) => void
Expected 3 arguments, but got 0.ts(2554)
index.d.ts(9, 3): An argument for 'dispatch' was not provided.
谢谢,
ThunkAction<()=>void, RootState, null, MoodAction>
似乎与中间件的定义不匹配。
我认为类似的东西
ThunkAction<()=>void, RootState, unknown, AnyAction>
//or
ThunkAction<()=>void, RootState, any, AnyAction>
会更好地工作,假设您的dispatch
类型包含thunk中间件的类型。
如果您正在使用官方推荐的redux工具包,请在此处记录设置正确的dispatch
类型:https://redux-toolkit.js.org/tutorials/typescript