Why is useGoogleReCaptcha returning undefined in my React App

I am trying to implement react-google-recaptcha-v3, but useGoogleReCaptcha() always returns undefined.

When I fill out the information for the form and click submit, I always get the alert('reCAPTCHA verification failed. Please try again.'); message. When I check the console I see Execute reCAPTCHA not yet available. If I console.log(executeReCaptcha); at the beginning of handleReCaptchaVerify, it just says undefined. I’m not sure what I’m doing wrong.

I’ve triple check the environment variables, and even tried to put the values directly into their respective fields, but I am still getting undefined for the value of executeReCaptcha.

Documentation also states “The executeRecaptcha function returned from the hook can be undefined when the recaptcha script has not been successfully loaded. You can do a null check to see if it’s available or not.” But why would it not successfully load? Is this an error in my implementation?

import axios from 'axios';
import React from 'react';
import { GoogleReCaptchaProvider, useGoogleReCaptcha } from 'react-google-recaptcha-v3';
import BottomContent from '../components/bottomContent';
import '../styles/contact.css';

function Contact() {
  const { executeReCaptcha } = useGoogleReCaptcha();

  const handleReCaptchaVerify = async () => {
    console.log(executeReCaptcha);
    if (!executeReCaptcha) {
      console.log('Execute reCAPTCHA not yet available');
      return null;
    }

    try {
      const token = await executeReCaptcha('submit');
      console.log('Verified token:', token);
      return token;
    } catch (error) {
      console.error('Error executing reCAPTCHA:', error);
      return null;
    }
  };

  const handleSubmit = async (event) => {
    event.preventDefault();

    const token = await handleReCaptchaVerify();
    if (!token) {
      alert('reCAPTCHA verification failed. Please try again.');
      return;
    }

    const formData = new FormData(event.target);
    const data = {
      name: formData.get('name'),
      email: formData.get('email'),
      subject: formData.get('subject'),
      message: formData.get('message'),
      recaptchaToken: token,
    };

    try {
      await axios.post('/api/send-email', data);
      alert('Your message has been sent!');
      event.target.reset();
    } catch (error) {
      console.error('Error sending message:', error);
      alert('Failed to send message. Please try again later.');
    }
  };

  return (
    <GoogleReCaptchaProvider reCaptchaKey={process.env.REACT_APP_RECAPTCHA_KEY}>
      <div className="ContactPage">
        <div className="showuppls">
          <h1 className='ContactHeader'>CONTACT US</h1>
          <div className='ContactDesc'><p>Reach out with your questions/inquiries about graphic design, photography & videography here!</p></div>
        </div>
        <div className="formContainer">
          <form onSubmit={ handleSubmit }>
            <div className="formRow">
              <div className="formLeft">
                <label htmlFor="name">NAME</label>
                <input type="text" id="name" name="name" required />
                <label htmlFor="email">EMAIL</label>
                <input type="email" id="email" name="email" required />
                <label htmlFor="subject">SUBJECT</label>
                <input type="text" id="subject" name="subject" />
              </div>
              <div className="formRight">
                <label htmlFor="message">YOUR MESSAGE</label>
                <textarea id="message" name="message" required></textarea>
              </div>
            </div>
            <button type="submit">SUBMIT</button>
          </form>
        </div>
        <BottomContent />
      </div>
    </GoogleReCaptchaProvider>
  );
}

export default Contact;

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật