react-google-recaptcha-v3 icon indicating copy to clipboard operation
react-google-recaptcha-v3 copied to clipboard

Recaptcha causing page rerendering

Open isferrei opened this issue 2 years ago • 5 comments

Hello guys, I hope you can help me with this issue. Using the google recaptcha inside my page, is causing an ininity renderings on it, making a lot of posts to google api.

image image

I'm not sure why its happening

isferrei avatar Aug 08 '22 20:08 isferrei

Avoid using inline functions for onVerify it causes verification process to run continuously.

waleed1892 avatar Sep 03 '22 20:09 waleed1892

Inline functions aren't the cause of this. The following only calls the reload api once.

<GoogleReCaptchaProvider reCaptchaKey={recaptchaKey}>
     <GoogleReCaptcha onVerify={(token) => {console.log(token)}} />
</GoogleReCaptchaProvider>

I also tried it with an inline promise with the same result.

<GoogleReCaptchaProvider reCaptchaKey={recaptchaKey}>
     <GoogleReCaptcha onVerify={(token) => new Promise((resolve, reject) => {console.log('test');resolve()})} />
</GoogleReCaptchaProvider>

Problems started happening for me when I used the formik libraries' setFieldValue method. This returns a promise, but for some reason causes the component to repeatedly reload when used with the GoogleReCaptcha component. It's likely the same issue happens with that setValue method call. Not sure on a workaround for this at the moment, but it might be possible to avoid the issue by wrapping the recaptcha bits in another component. Might reload only that component then and not the whole form, though I suspect that won't work. Otherwise will need to roll a custom solution using useGoogleReCaptcha() to manually run the recaptcha calls.

jaandrews avatar Feb 08 '23 23:02 jaandrews

fixed it by changing to onVerify={setRecaptchaValue} instead

Keith-Hon avatar Mar 16 '23 13:03 Keith-Hon

Use it like this: { !token && <GoogleRecaptcha onVerify={token => setToken(token)} /> }

vahidei avatar Aug 09 '23 20:08 vahidei

  const onVerify = useCallback((googleReCaptchaToken: string) => {
    console.log(googleReCaptchaToken)
    setToken(googleReCaptchaToken)
  }, [])
<GoogleReCaptcha onVerify={onVerify} />

mleister97 avatar Nov 29 '23 17:11 mleister97