1

I've run into an issue in my React project where the Google reCaptcha I want to include at the end of a form does not initially appear. If the webpage is refreshed, it appears as expected, but if a user navigates to the form naturally through the site, it is missing and the form cannot be completed.

Even when stripping down the code to the bare minimum like so, the recaptcha does not appear until the page is refreshed:

import React, { useRef } from "react"
import ReCAPTCHA from "react-google-recaptcha"

const RecaptchaTest = () => {
  const captchaRef = useRef(null)
  return (
    <div>
      <ReCAPTCHA
        sitekey={process.env.GATSBY_RECAPTCHA_SITE_KEY}
        ref={captchaRef}
      />
    </div>
  )
}

export default RecaptchaTest

I believe that the issue may have something to do with the appropriate google cookies not being generated in time but have yet to find a proper solution. Could it have something to do with the react-google-recaptcha package? Any help or advice would be greatly appreciated. My site was built using Gatsby.

3
  • I too have the same problem with my Gatsby project. Our site is hosted on Firebase. It works fine when we tested by hosting the same site using gatsby cloud. I tried with react-recaptcha also, and it has the same problem.
    – Saran
    Commented Apr 21, 2023 at 3:50
  • I recommend using react-recaptcha instead. It works pretty well for me. Commented Apr 26, 2023 at 7:15
  • What is the error appearing ? Are you sure the environment variable is fed correctly to the component ? Have you tested with other libraries ? [npmjs.com/package/react-google-recaptcha-v3] (example) Commented Apr 26, 2023 at 13:49

1 Answer 1

0

One solution is you can try onloadCallBack prop that will trigger only after loading the reCAPTCHA

ref={captchaRef}
 asyncScriptOnLoad={() => 
{ console.log("reCAPTCHA loaded successfully") }} />
2
  • There is not onloadCallback method in ReCAPTCHA of react-google-recaptcha
    – Saran
    Commented Apr 29, 2023 at 4:52
  • asyncScriptOnLoad...optional callback when the google recaptcha script has been loaded can you try this
    – Sweety SK
    Commented Apr 30, 2023 at 9:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.