【问题标题】:How do I type onSuccess data in React Query?如何在 React Query 中输入 onSuccess 数据?
【发布时间】:2022-11-12 10:29:11
【问题描述】:

我有 API 调用函数(httpClientaxios 实例)


interface IRegisterResponse {
  accessToken: string;
}

export const register = async ({
  name,
  password,
  token,
}: IRegisterParams) =>
  await httpClient.post<IRegisterResponse>('register', {
    name,
    password,
    token,
  });

我有处理这个 API 调用的 useMutation 钩子

  const { mutate: registerMutation } = useMutation(
    ['register'],
    register,
    {
      onSuccess: ({ accessToken }) => console.log(accessToken),
    }
  );

但是在onSuccess 回调中,它会突出显示accessToken 并带有消息Property 'accessToken' does not exist on type 'AxiosResponse&lt;IRegisterResponse, any&gt;'

我做错了什么,我应该如何输入?

【问题讨论】:

    标签: reactjs typescript axios react-query


    【解决方案1】:

    您正在返回 axios 响应,而不是数据

    onSuccess: ({ data: { accessToken } }) => console.log(accessToken)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 2022-12-15
      • 2021-11-05
      • 1970-01-01
      • 2020-12-18
      相关资源
      最近更新 更多