使用 Redux-Toolkit,我尝试在 createAsyncThunk
中使用 ThunkAPI 和分派(dispatch),但由于类型错误而被拒绝。不确定如何解决这个问题。
我的商店:
export const store = configureStore({
reducer: rootReducer,
middleware: [...getDefaultMiddleware()],
});
我的行动:
export const tester = createAsyncThunk(
'tester',
async (testData, {dispatch}) => {
await dispatch(load(true));
const final = await someExternalFunc(testData)
return final;
}
);
任何帮助将不胜感激。
最佳答案
根据您的评论,您没有正确调用 thunk。
调用 test()
返回一个 Action ,然后你应该调度这个 Action :
const fetchTodo = createAsyncThunk("todo/fetchTodo", async (args, thunkAPI) => {
console.log(thunkAPI, "thunkAPI");
const response = await todoAPI();
return JSON.stringify(response);
});
dispatch(test(testData));
关于javascript - Redux-Toolkit createAsyncThunk Dispatch 显示为未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62778386/