1

I'm new with typescript i use react-query try to us mutate but it cause an error

TS2554: Expected 1-2 arguments, but got 3.

iterface:

interface ChangePassword{
    email: string;
    password: string;
    confirmPassword: string
}

function:

 const changePasswordOnClick = useMutation<Record<string, string>, unknown, ChangePassword>(
        () => axios.post(
            url,
            { email, password, new_password}, // all fine
        ),
        {
            onSuccess: (req) => {
                console.log('Request', req)

            },

            onError: (newLogin) => {
                console.log('onError', newLogin);
            },
        },
    );

Problem (just piece of problem code):

onSubmit={async (data,
{setSubmitting, resetForm})=>{setSubmitting(true)                                 
                  changePasswordOnClick.mutate(
                     data.email, 
                    data.password, 
                    data.confirmPassword)} // TS2554: Expected 1-2 arguments, but got 3.

Why i'm getting this error? how to fix it?

1 Answer 1

1

Because in mutate type we have

(variables: TVariables, options?: MutateOptions) => Promise<TData>

and you should change mutationFn:

const changePasswordOnClick = useMutation<Record<string, string>, unknown, ChangePassword>(
  (params: ChangePassword) => axios.post(url, params),
  {
    onSuccess: (req) => {
      console.log("Request", req)
    },

    onError: (newLogin) => {
      console.log("onError", newLogin)
    },
  }
)

and mutate calling also:

changePasswordOnClick.mutate({
   confirmPassword: data.confirmPassword,
   email: data.email,
   password: data.password,
})

To match to the type

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.