我对redux-thunk(没有反应,只有控制台应用程序)进行了示例测试,代码在这里:
interface IStartLoadAction extends Action<"START"> {
}
interface IEndLoadAction extends Action<"END"> {
}
type LoadActionTypes = IStartLoadAction | IEndLoadAction;
function startLoad(): LoadActionTypes {
return {type: "START"};
}
function endLoad(): LoadActionTypes {
return {type: "END"};
}
const loadReducer: Reducer<ILoadState | undefined, LoadActionTypes> = (
state = initLoadState,
action: LoadActionTypes) => {
switch (action.type) {
case "START":
return {isLoading: !state.isLoading};
case "END":
return {isLoading: !state.isLoading};
default:
return {isLoading: false};
}
};
const rootReducer = combineReducers({
loader: loadReducer,
});
const store = createStore(rootReducer, applyMiddleware(Thunk));
const fetchData = (): ThunkAction<Promise<void>, ILoadState, null, AnyAction> => {
return async (dispatch: ThunkDispatch<{}, {}, AnyAction>): Promise<void> => {
return new Promise<void>((resolve) => {
dispatch(startLoad());
console.log('start....');
setTimeout(() => {
dispatch(<some actions>);
}, 2000);
dispatch(endLoad());
console.log('end....');
resolve();
});
};
};
但是当我打电话给store.dispatch(fetchData());
时,它返回一个错误:
'ThunkAction,ILoadState,null, 无法将“ AnyAction>”分配给“ AnyAction”类型的参数。
如何使其工作?谢谢(我不会在react env中使用它,仅限服务器端)
Redux官方网站的示例是“ js”,而不是“ ts”,因此我不知道如何翻译。