简体   繁体   中英

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. 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.

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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