Overview
GoogleReCaptchaProvider is the root component that wraps your application (or a part of it) to initialize Google reCAPTCHA v3. It loads the reCAPTCHA script, manages the reCAPTCHA instance, and provides context to child components through React Context.
Source: src/google-recaptcha-provider.tsx:63
Import
Props
Your Google reCAPTCHA v3 site key. Get this from the Google reCAPTCHA Admin Console.
The language code for the reCAPTCHA widget (e.g.,
"en", "fr", "es"). If not specified, the user’s browser language will be used.If
true, loads the reCAPTCHA script from recaptcha.net instead of google.com. Useful for regions where google.com is blocked.If
true, uses Google reCAPTCHA Enterprise instead of the standard version. Requires an Enterprise site key.Configuration options for the script tag that loads reCAPTCHA.
Configuration for explicit rendering mode. When provided, reCAPTCHA will render into a specific container element.
The child components that will have access to the reCAPTCHA context.
Context Value
The provider creates a context with the following value (accessible via useGoogleReCaptcha or GoogleReCaptchaConsumer):Function to execute reCAPTCHA and get a token. Returns
undefined until reCAPTCHA is fully loaded.- action: Optional string describing the action being performed (e.g.,
"login","submit_form") - Returns: Promise that resolves to the reCAPTCHA token
- Throws: Error if reCAPTCHA is not loaded when called
The container element used for explicit rendering, if provided.
Usage Examples
Basic Usage
With Language and Custom Script Props
Using reCAPTCHA Enterprise
Using recaptcha.net Domain
Explicit Rendering with Custom Container
Consumer
The provider also exportsGoogleReCaptchaConsumer for consuming the context in class components:
TypeScript Interfaces
See TypeScript Interfaces for complete type definitions.Cleanup
The provider automatically cleans up when unmounted:- Removes the reCAPTCHA script tag
- Removes the reCAPTCHA badge from the DOM
- Clears internal reCAPTCHA configuration
Related APIs
- useGoogleReCaptcha - Hook to access the reCAPTCHA context
- GoogleReCaptcha - Component for automatic token generation
- withGoogleReCaptcha - HOC for class components
- TypeScript Interfaces - Type definitions