DEV Community

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

Posted on

2 3

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

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup πŸš€

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay