javascript - Redux-Toolkit createAsyncThunk Dispatch is showing as undefined - Stack Overflow

Using Redux-Toolkit, I am trying to use ThunkAPI & dispatch inside an createAsyncThunk but I am get

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.

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.

Share Improve this question edited Jul 7, 2020 at 15:19 Dennis Vash 54.1k12 gold badges119 silver badges132 bronze badges asked Jul 7, 2020 at 15:12 saisai 5331 gold badge5 silver badges12 bronze badges 7
  • Please make a reproducible example: How to create a Minimal, Reproducible Example, there is no indication that it is the source of the error, better make a sandbox codesandbox, and please add actual code instead of images. – Dennis Vash Commented Jul 7, 2020 at 15:19
  • Instead of destructuring the thunkAPI object could you pass the full object in and see if the dispatch method is defined there ? – Omar Nasr Commented Jul 7, 2020 at 15:22
  • @OmarNasr, even If I have thunkAPI instead of {dispatch} I am getting thunkAPI as undefined when I do a console.log. – sai Commented Jul 7, 2020 at 15:32
  • Where and how are you dispatching this thunk? (Also, you shouldn't need to call getDefaultMiddleware() yourself in that store setup snippet if you're not customizing anything - you can leave it out entirely.) – markerikson Commented Jul 7, 2020 at 15:34
  • So you don't calling tester right, show the whole code. – Dennis Vash Commented Jul 7, 2020 at 15:35
 |  Show 2 more ments

1 Answer 1

Reset to default 3

According to your ment, 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));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745456260a4628498.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信