简体   繁体   中英

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

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

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

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