DEV Community

UncleZeze πŸ‡ΏπŸ‡¦
UncleZeze πŸ‡ΏπŸ‡¦

Posted on

React Hooks Noob, please help...

I have a sign up form that accepts basic username/password data. This needs to authenticate against a graphql endpoint using a custom hook. React Hooks rules dictate, you cannot call a hook within an event handler. How else can the custom hook be invoke to perform the authentication once the user submits the form?

Top comments (2)

Collapse
 
hellozeze profile image
UncleZeze πŸ‡ΏπŸ‡¦

Obviously, as the docs state, you are not allowed to execute hooks inside of an event handler and other forbidden things.

As a workaround, you can execute fetch... Ultimately, what I am looking for is interaction with the graphql endpoint. So a simple fetch does the job.

Collapse
 
chico1992 profile image
chico1992

Do you have a code snippet or a repo you can show as without code it's pretty hard to help you
I could guess that you're using some sort of library(apollo) for graphql if this is the case something like the following should work

const SubmitForm = () =>{
    const [mutation] = useMutation();
    return(
        <form onSubmit={()=>{mutation()}}>
            {/* some more components here */}
        </form>
    );
}

this is in no way working code but the usage should be clear