1

I've read through several posts that have had similar issues with ReCAPTCHA v2 but I'm still unable to figure out the issue.

I followed the npm and google documentation by installing npm install --save react-google-recaptcha and including <script src="https://www.recaptcha.net/recaptcha/api.js" async defer></script> within the body of the index.html and the page in which i want the reCaptcha to be shown.

But for some reason the reCaptcha will not show up on the page; with or without the code to make the reCaptcha functional.

Edit

The reCaptcha v2 was showing on the screen without changing anything with the code for a few moments and disappeared. I restarted VSCode and Github Desktop but the issue hasn't been resolved.

Contact.jsx

import React from 'react'
import Stockpic15 from '../img/stockpic15.jpg';
import ReCAPTCHA from "react-google-recaptcha";
import REACT_APP_SITE_KEY from '../.env'

const Contact = () => {
  
  const onChange = () => {

  }

  return (
  <div className=''>  
   <script src="https://www.recaptcha.net/recaptcha/api.js" async defer></script>
    ....
      <div className='w-2/3 pb-5'>
        <img src={Stockpic15} alt="" />
      </div>
        <form className='flex flex-col justify-center items-center sm:space-y-8 mobile:space-y-10 mobile:text-lg tablet:text-2xl'>
          <input className='contact-input' type='text' name='first-name' placeholder='Your First Name' required/>
          <input className='contact-input' type='text' name='last-name' placeholder='Your Last Name' required/>
          <input className='contact-input' type='email' name='email' placeholder='Your Email Address' required/>
          <textarea className='border-black border-2 rounded-md block w-full resize' name="message" rows="4" placeholder='Message'></textarea>
          <button type="submit" href="#_" className="relative inline-block text-lg group">
            ...
          </button>
           <ReCAPTCHA 
          sitekey={REACT_APP_SITE_KEY}
          oncChange={onChange}
          />
      </form>
    </div>
  </div>
  )}

  export default Contact

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <script src="https://www.recaptcha.net/recaptcha/api.js" async defer></script>
  </body>
</html>
7
  • 1
    Why are you including the script with reCaptcha two times?
    – jcubic
    Commented Feb 1, 2023 at 15:17
  • documentation states it needs to be included in the index.html and wherever the reCAPTCHA is being used developers.google.com/recaptcha/docs/…
    – Jeremiah
    Commented Feb 1, 2023 at 15:29
  • 1
    But you can't use it in both places. If you use it two times second load will delete the first instance. That's probably why reCaptacha displays for a second. This is the time when your component is created and new version if the file is loaded.
    – jcubic
    Commented Feb 1, 2023 at 15:31
  • 1
    I don't see this information in the documentation. Can you quote the text so I can find it?
    – jcubic
    Commented Feb 1, 2023 at 15:32
  • @jcubic deleted the script from the index.html and it worked. I'm not sure whether I misread the documentation or not. Other posts I've seen have even said the same thing so this is odd
    – Jeremiah
    Commented Feb 1, 2023 at 15:35

0

Browse other questions tagged or ask your own question.