Redux-Toolkit createAsyncThunk Dispatch is showing as undefined

戏子无情 提交于 2020-07-10 07:01:19

问题


Using Redux-Toolkit, I am trying to use ThunkAPI & dispatch inside an createAsyncThunk but I am getting rejected because of type error. 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

Any help will be really appreciated.


回答1:


According to your comment, you are not calling the thunk right.

Calling test() returns an action, then you should dispatch the action:

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

dispatch(test(testData));


来源:https://stackoverflow.com/questions/62778386/redux-toolkit-createasyncthunk-dispatch-is-showing-as-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!