redux 应为0个参数,但在将参数传递给createAsyncThunk函数时得到1个

5t7ly7z5  于 2022-11-12  发布在  其他
关注(0)|答案(1)|浏览(171)

这是我的服务,我在其中调用api(带参数)

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

这是我调用该服务的操作

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

这是减速器

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

选择器

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

这就是我如何使用

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

而得到这个错误
应为0个参数,但得到1个

jvlzgdj9

jvlzgdj91#

如果在上面指定了泛型,则必须完全指定:

export const getResult=createAsyncThunk<ResultType, ArgumentType>(

否则,它将退回到void,这意味着“无参数”。

相关问题