t49tran / react-google-recaptcha-v3

Google Recaptcha V3 integration for React
MIT License
434 stars 91 forks source link

Add executeRecaptcha as depedency on useCallback #119

Closed chemiadel closed 2 years ago

chemiadel commented 2 years ago

on #react-hook-usegooglerecaptcha-recommended-approach

we need to add executeRecaptcha as dependency on useCallback, executeRecaptcha is not defined at first

new code

  // Create an event handler so you can call the verification on button click event or form submit
  const handleReCaptchaVerify = useCallback(async () => {
    if (!executeRecaptcha) {
      console.log("Execute recaptcha not yet available");
      return;
    }

    const token = await executeRecaptcha("yourAction");
    // Do whatever you want with the token
    console.log({ token });
  }, [executeRecaptcha]); // adding executeRecaptcha as dependency 
michaeljrogers1994 commented 2 years ago

I was also having issues with executeRecaptcha being undefined until I added it as a dependency. This resolved it.

t49tran commented 2 years ago

Thanks for this, I will close the PR as the problem has been fixed in #117