0

I try to use react query useMutation with typescript and still getting error on args when i want to trigger my function. I'm new in typescript i try to googled, and on stackoverflow also give me infomation how to type useMutation. But i still do not understand why i'm getting error TS2345: Argument of type 'string' is not assignable to parameter of type 'Login'

interface Login{
    username: string,
    password: string

}

interface Token{
    access_token: string,
    refresh_token: string
}


const loginToServer = useMutation<Token, unknown,  Login>(newLogin => axios.post(url, {
        username: username,
        password: password
    }),
)


<Button label="Sign In" icon="pi pi-user" 
       onClick={() => loginToServer.mutate(username, password)}/> // revieve an error TS2345: Argument of type 'string' is not assignable to parameter of type 'Login'.


1 Answer 1

2

You need to pass your variables to mutate as an object of shape Login, so wrap your username, password in { and }).

<Button label="Sign In" icon="pi pi-user" 
       onClick={() => loginToServer.mutate( { username, password } )}/> 
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.