简体   繁体   English

Redux-Toolkit createAsyncThunk Dispatch 显示为未定义

[英]Redux-Toolkit createAsyncThunk Dispatch is showing as undefined

Using Redux-Toolkit, I am trying to use ThunkAPI & dispatch inside an createAsyncThunk but I am getting rejected because of type error.使用 Redux-Toolkit,我尝试在 createAsyncThunk 中使用createAsyncThunk和调度,但由于类型错误而被拒绝。 Not sure how to resolve this.不知道如何解决这个问题。

my store:我的商店:

export const store = configureStore({ 
    reducer: rootReducer, 
    middleware: [...getDefaultMiddleware()],
});

my Action:我的行动:

export const tester = createAsyncThunk(
    'tester',
    async (testData, {dispatch}) => { 
        await dispatch(load(true));
        const final = await someExternalFunc(testData)
        return final;
    }
);

but, I am getting output as但是,我得到 output 作为在此处输入图像描述

Any help will be really appreciated.任何帮助将不胜感激。

According to your comment, you are not calling the thunk right.根据您的评论,您没有正确调用 thunk。

Calling test() returns an action, then you should dispatch the action:调用test()返回一个动作,然后你应该调度这个动作:

const fetchTodo = createAsyncThunk("todo/fetchTodo", async (args, thunkAPI) => {
  console.log(thunkAPI, "thunkAPI");
  const response = await todoAPI();
  return JSON.stringify(response);
});

dispatch(test(testData));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM