Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I type onSuccess data in React Query?

I have API call function (httpClient is axios instance)


interface IRegisterResponse {
  accessToken: string;
}

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

And I have useMutation hook that handles this API call form me

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

But in onSuccess callback it highlights accessToken with message Property 'accessToken' does not exist on type 'AxiosResponse<IRegisterResponse, any>'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What am I doing wrong and how should I type it?

>Solution :

You are returning axios response, not data

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

or

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading