React Google reCAPTCHA Wrapper

3.1.0 · active · verified Tue Apr 21

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

import React from 'react';
import ReactDOM from 'react-dom';
import ReCAPTCHA from 'react-google-recaptcha';

const RECAPTCHA_SITE_KEY = process.env.RECAPTCHA_SITE_KEY ?? '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXwpTqiT'; // Example key

function App() {
  function onChange(value) {
    console.log('Captcha value:', value);
    // In a real application, send this 'value' (token) to your backend for verification.
  }

  function onExpired() {
    console.log('reCAPTCHA challenge expired.');
  }

  function onErrored(error) {
    console.error('reCAPTCHA challenge errored:', error);
  }

  return (
    <div>
      <h1>React Google reCAPTCHA Example</h1>
      <p>Please complete the reCAPTCHA to proceed.</p>
      <ReCAPTCHA
        sitekey={RECAPTCHA_SITE_KEY}
        onChange={onChange}
        onExpired={onExpired}
        onErrored={onErrored}
        theme="light"
        size="normal"
      />
      <p>Note: This sitekey is for demonstration only. Use your own client site key registered with Google reCAPTCHA.</p>
    </div>
  );
}

ReactDOM.render(<App />, document.getElementById('root') || document.createElement('div'));

view raw JSON →