WebArtisan
WebArtisan

Reputation: 4227

How to pass arguments to createAsyncThunk?

Accordingly to documentation I can send some params into createAsyncThunk

export const someCustomAsyncThunk = createAsyncThunk(
    'counter/customFetch',
    async (someParam, thunkAPI) => { //<-- someParam ?
        const response = await fetch('https://someapi');

        return await response.json();
    }
);
extraReducers: {
   [someCustomAsyncThunk.fulfilled.type]: (state, action) => {
     // state dispatching gonna be there
   },

And I have an action somewhere inside my render:

onClick={() => dispatch(someCustomAsyncThunk(38746))}

but I'm receiving an error: Expected 0 arguments, but got 1.

What did I missed?

Upvotes: 2

Views: 2507

Answers (1)

WebArtisan
WebArtisan

Reputation: 4227

solution not obvious, but in general, all is need is just providing types for arguments:

export const someCustomAsyncThunk = createAsyncThunk(
    'counter/customFetch',
    async (someParam: number, thunkAPI) => { <-- put types here

Upvotes: 4

Related Questions