GoogleReCaptcha Context has not yet been implemented

See original GitHub issue

Thanks for creating this library! I have run into a small issue however, and I’m curious whether my use case is supported.

The executeRecaptcha function returned by useGoogleReCaptcha throws a GoogleReCaptcha Context has not yet been implemented error when I try to use GoogleReCaptchaProvider on specific pages, rather than the entire app (in ReactDOM.render). I only want to use it on my app’s signup and forgot password pages, because I don’t want Google’s reCAPTCHA logo in the lower right corner of every page of the app. (It’s especially in the way on mobile.)

I’m guessing this is a consequence of using React Context, so you can’t use the provider and the hook/consumer in the same component. If so, I think the error message could be improved, such as You can only call useGoogleReCaptcha in a component that is wrapped in GoogleReCaptchaProvider. It would also be helpful if the documentation provided guidance or an example of using the library on specific pages.

import React from 'react';
import { GoogleReCaptchaProvider, useGoogleReCaptcha } from 'react-google-recaptcha-v3';

const { REACT_APP_RECAPTCHA_SITE_KEY } = process.env;

export default function SignupPage(): React.ReactElement {
  const { executeRecaptcha } = useGoogleReCaptcha();
  const onSubmit = React.useCallback(
    (event: React.FormEvent) => {
      event.preventDefault();
      if (!executeRecaptcha) return;
      (async () => {
        try {
          const token = await executeRecaptcha('signup'); // throws context error
          // ...
        } catch (e) {
          // ...
        }
      })();
    },
    [executeRecaptcha]
  );
  return (
    <GoogleReCaptchaProvider reCaptchaKey={REACT_APP_RECAPTCHA_SITE_KEY} scriptProps={{ async: true }}>
      <form onSubmit={onSubmit} noValidate>
        {/* ... */}
      </form>
    </GoogleReCaptchaProvider>
  );
}

This version seems to work:

export default function SignupPage(): React.ReactElement {
  return (
    <GoogleReCaptchaProvider reCaptchaKey={REACT_APP_RECAPTCHA_SITE_KEY} scriptProps={{ async: true }}>
      <SignupForm />
    </GoogleReCaptchaProvider>
  );
}

function SignupForm(): React.ReactElement {
  const { executeRecaptcha } = useGoogleReCaptcha();
  const onSubmit = React.useCallback(
    (event: React.FormEvent) => {
      event.preventDefault();
      if (!executeRecaptcha) return;
      (async () => {
        try {
          const token = await executeRecaptcha('signup');
          // ...
        } catch (e) {
          // ...
        }
      })();
    },
    [executeRecaptcha]
  );
  return (
    <form onSubmit={onSubmit} noValidate>
      {/* ... */}
    </form>
  );
}

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:7
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

15reactions
phatNfqAsiacommented, Sep 1, 2021

Hello @t49tran , based on @trevorr example I suppose the snippet in https://github.com/t49tran/react-google-recaptcha-v3#react-hook-usegooglerecaptcha-recommended-approach should be updated right? with empty dependencies this will also throw Execute recaptcha not yet available . It should be written this way:

import {
  GoogleReCaptchaProvider,
  useGoogleReCaptcha
} from 'react-google-recaptcha-v3';

const YourReCaptchaComponent = () => {
  const { executeRecaptcha } = useGoogleReCaptcha();

  // 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
+  }, [executeRecaptcha]);

  // You can use useEffect to trigger the verification as soon as the component being loaded
  useEffect(() => {
    handleReCaptchaVerify();
  }, [handleReCaptchaVerify]);

  return <button onClick={handleRecaptchaVerify}>Verify recaptcha</button>;
};

ReactDom.render(
  <GoogleReCaptchaProvider reCaptchaKey="[Your recaptcha key]">
    <YourReCaptchaComponent />
  </GoogleReCaptchaProvider>,
  document.getElementById('app')
);
2reactions
biniyammogescommented, Apr 6, 2022

is there a way to hide the recaptcha logo on the bottom right corner?

add these code to your css

.grecaptcha-badge {
   display: none;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to verify the token and get score using react-google ...
The score is associated with the token but that'll be produced when you're doing the actual backend verification request with the token ...
Read more >
Introducing reCAPTCHA v3: the new way to stop bots
First, you can set a threshold that determines when a user is let through or when further verification needs to be done, for...
Read more >
How to implement reCAPTCHA in a React application
In this article, we'll demonstrate how to implement reCAPTCHA v2 in a ... that gets called when the Google reCAPTCHA script has been...
Read more >
How to Add Google reCAPTCHA v3 in a Next.js Form
I believe that the reader already has a basic understanding of the ... In this article, we are implementing Google reCAPTCHA v3 on...
Read more >
Configuring Multi-factor authentication | reCAPTCHA Enterprise
MFA is available for the score-based site keys and is not available for the ... Important: The phone number has to be valid...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found