react-google-recaptcha is a React component wrapper for Google reCAPTCHA v2, simplifying its integration into web applications. Currently stable at version 3.1.0, it abstracts away the asynchronous loading of the reCAPTCHA JavaScript API and provides a declarative API for rendering and interacting with reCAPTCHA widgets. The library offers props for customizing widget appearance (theme, size, type), language, and badge positioning, as well as callbacks for success (`onChange`), expiration (`onExpired`), and errors (`onErrored`). It also exposes an instance API via React refs for programmatic actions like `reset()`, `getValue()`, `execute()`, and `executeAsync()`, which are particularly useful for invisible reCAPTCHA implementations. Its primary differentiator is streamlining the often complex reCAPTCHA setup into a standard React component lifecycle, reducing boilerplate and common integration pitfalls.
npm install react-google-recaptchaVerified import paths — ran on the pinned version, not inferred.
Initializes and renders a visible Google reCAPTCHA v2 widget, logging the token on successful completion, expiration, or error. Requires a `root` element or will create one.
Implement a server-side endpoint to receive the reCAPTCHA token and verify it with `https://www.google.com/recaptcha/api/siteverify` before processing any sensitive user action.
Call `recaptchaRef.current.reset()` in your component's logic after your backend successfully validates the token and completes the user action.
Ensure you call `recaptchaRef.current.execute()` (typically on a form submission) when using `size="invisible"`. Integrate this with your form's submission handler.
Set the `isolated={true}` prop on your `ReCAPTCHA` component if there's a possibility of other reCAPTCHA instances on the host page where your component is embedded.If reCAPTCHA v3 functionality is required, consider using a library specifically designed for v3 or integrating the v3 API directly.
Ensure each `ReCAPTCHA` component renders into a unique DOM location. If embedding, consider using the `isolated` prop set to `true`.
Double-check the `sitekey` value against your Google reCAPTCHA admin console and ensure the domain registration matches. Verify your browser console for network errors or CSP warnings, and ensure your CSP allows loading scripts from `www.google.com/recaptcha/api.js`.
Ensure the ref is properly initialized and the component is mounted before accessing `recaptchaRef.current`. For functional components, use `useRef` and ensure the code accessing the ref runs after the component has rendered (e.g., within an `useEffect` or an event handler triggered after mount).