Home > Enterprise >  expected 0 arguments, but got 1 while passing arguments to the createAsyncThunk function
expected 0 arguments, but got 1 while passing arguments to the createAsyncThunk function

Time:09-26

This is my service in which i'm calling api(with arguments)

    const battle = async (obj:any): Promise<result[]> =>{
    const data=await fetch(`${API_URL}`,{
    method: "POST",
  
    // Adding body or contents to send
    body: JSON.stringify({
      "1stArg": obj.arg1,
      "2ndArg": obj.arg2
    }),
      
    // Adding headers to the request
    headers: {
        "Content-type": "application/json; charset=UTF-8"
    }
  });
   const res = await data.json()
   return res;
}

This is my action in which I'm calling that service

export const getResult=createAsyncThunk<result[]>(
  'testing/getResult',
  async(obj:any)=>{
    const data= await services.battle(obj);
    return data;
  }
);

This is the reducer

  builder.addCase(getResult.pending, (state) => ({
    ...state,
    result: null,
  }));

  builder.addCase(getResult.rejected, (state) => ({
    ...state,
    result: null,
  }));

  builder.addCase(getResult.fulfilled, (state, action) => ({
    ...state,
    result: action.payload,
  }));

Selector

export const getFinalResult = (state: RootState) => state.monsters.result;

This is how I'm using

const result=useSelector(getFinalResult);
const handleStartBattleClick = () => {
    let obj={
        fistArg,2ndArg
    }
    dispatch(getResult(obj))
}

and getting this error

Expected 0 arguments, but got 1

CodePudding user response:

If you specify that generic up there, you have to do so fully:

export const getResult=createAsyncThunk<ResultType, ArgumentType>(

otherwise it will fall back to void, which means "no argument".

  • Related