Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-google-recaptcha

dozoisch3.4mMIT3.1.0TypeScript support: definitely-typed

React Component Wrapper for Google reCAPTCHA

react, react-component, captcha, recaptcha, google-recaptcha

readme

react-google-recaptcha

Build Status npm version npm downloads

Edit react-google-recaptcha example

React component for Google reCAPTCHA v2.

Installation

npm install --save react-google-recaptcha

Usage

All you need to do is sign up for an API key pair. You will need the client key then you can use <ReCAPTCHA />.

The default usage imports a wrapped component that loads the google recaptcha script asynchronously then instantiates a reCAPTCHA the user can then interact with.

Code Example:

import ReCAPTCHA from "react-google-recaptcha";

function onChange(value) {
  console.log("Captcha value:", value);
}

ReactDOM.render(
  <ReCAPTCHA
    sitekey="Your client site key"
    onChange={onChange}
  />,
  document.body
);

Component Props

Properties used to customise the rendering:

Name Type Description
asyncScriptOnLoad func optional callback when the google recaptcha script has been loaded
badge enum optional bottomright, bottomleft or inline. Positions reCAPTCHA badge. Only for invisible reCAPTCHA
hl string optional set the hl parameter, which allows the captcha to be used from different languages, see reCAPTCHA hl
isolated bool optional For plugin owners to not interfere with existing reCAPTCHA installations on a page. If true, this reCAPTCHA instance will be part of a separate ID space. (default: false)
onChange func The function to be called when the user successfully completes the captcha
onErrored func optional callback when the challenge errored, most likely due to network issues.
onExpired func optional callback when the challenge is expired and has to be redone by user. By default it will call the onChange with null to signify expired callback.
sitekey string The API client key
size enum optional compact, normal or invisible. This allows you to change the size or do an invisible captcha
stoken string optional set the stoken parameter, which allows the captcha to be used from different domains, see reCAPTCHA secure-token
tabindex number optional The tabindex on the element (default: 0)
type enum optional image or audio The type of initial captcha (defaults: image)
theme enum optional light or dark The theme of the widget (defaults: light). See example

Component Instance API

The component instance also has some utility functions that can be called. These can be accessed via ref.

  • getValue() returns the value of the captcha field
  • getWidgetId() returns the recaptcha widget Id
  • reset() forces reset. See the JavaScript API doc
  • execute() programmatically invoke the challenge
  • executeAsync() programmatically invoke the challenge and return a promise that resolves to the token or errors(if encountered).
    • alternative approach to execute() in combination with the onChange() prop - example below

Example:

const recaptchaRef = React.createRef();
...
onSubmit = () => {
  const recaptchaValue = recaptchaRef.current.getValue();
  this.props.onSubmit(recaptchaValue);
}
render() {
  return (
    <form onSubmit={this.onSubmit} >
      <ReCAPTCHA
        ref={recaptchaRef}
        sitekey="Your client site key"
        onChange={onChange}
      />
    </form>
  )
}

Invisible reCAPTCHA

▶ Codesandbox invisible example

See the reCAPTCHA documentation to see how to configure it.

With the invisible option, you need to handle things a bit differently. You will need to call the execute method yourself.

import ReCAPTCHA from "react-google-recaptcha";

const recaptchaRef = React.createRef();

ReactDOM.render(
  <form onSubmit={() => { recaptchaRef.current.execute(); }}>
    <ReCAPTCHA
      ref={recaptchaRef}
      size="invisible"
      sitekey="Your client site key"
      onChange={onChange}
    />
  </form>,
  document.body
);

Additionally, you can use the executeAsync method to use a promise based approach.

import ReCAPTCHA from "react-google-recaptcha";


const ReCAPTCHAForm = (props) => {
  const recaptchaRef = React.useRef();

  const onSubmitWithReCAPTCHA = async () => {
    const token = await recaptchaRef.current.executeAsync();

    // apply to form data
  }

  return (
    <form onSubmit={onSubmitWithReCAPTCHA}>
      <ReCAPTCHA
        ref={recaptchaRef}
        size="invisible"
        sitekey="Your client site key"
      />
    </form>
  )

}

ReactDOM.render(
  <ReCAPTCHAForm />,
  document.body
);

Advanced usage

Global properties used by reCaptcha

useRecaptchaNet: If google.com is blocked, you can set useRecaptchaNet to true so that the component uses recaptcha.net instead.

enterprise: if you want to use Google Enterprise Recaptcha, instead of the free version, set enterprise to true.

Example global properties:

window.recaptchaOptions = {
  useRecaptchaNet: true,
  enterprise: true,
};

CSP Nonce support

window.recaptchaOptions = {
  nonce: document.querySelector('meta[name=\'csp-nonce\']').getAttribute('content'),
};

ReCaptcha loading google recaptcha script manually

You can also use the barebone components doing the following. Using that component will oblige you to manage the grecaptcha dep and load the script by yourself.

import { ReCAPTCHA } from "react-google-recaptcha";

const grecaptchaObject = window.grecaptcha // You must provide access to the google grecaptcha object.

render(
  <ReCAPTCHA
    ref={(r) => this.recaptcha = r}
    sitekey="Your client site key"
    grecaptcha={grecaptchaObject}
  />,
  document.body
);

Hiding the Recaptcha

According to the google docs you are allowed to hide the badge as long as you include the reCAPTCHA branding visibly in the user flow. Please include the following text:

This site is protected by reCAPTCHA and the Google
    <a href="https://policies.google.com/privacy">Privacy Policy</a> and
    <a href="https://policies.google.com/terms">Terms of Service</a> apply.

If you wish to hide the badge you must add:

.grecaptcha-badge { visibility: hidden; }

to your css.

changelog

v3.1.0 - Sat 3 Jun 2023 0:30 ET

v3.0.0-alpha.1 - Mon 23 Nov 2020 22:25 ET

v2.1.0 - Fri 5 Jun 2020 22:05 PST

  • Add promise based Execution (#163)

v2.0.1 - Sat 14 Sep 2019 12:00 PST

  • Fix onChange was using the function passed at mount time (#154)

v2.0.0-rc.1 - Sat 3 Aug 2019 8:00 PST

  • Remove lang and removeOnUnmount global options (#143)
  • Upgrade to node 8, 10, 12 (#143)
  • Update to react-async-script 1.1 (#143)

v1.1.0 - Sun 14 Jul 2019 10:56 PST

v1.0.5 - Mon 12 Nov 2018 9:25 PST

v1.0.4 - Thu 27 Sep 2018 15:14 PST

  • add .babelrc to .npmignore (#111)

v1.0.2 - Wed, 5 Sep 2018 13:21:00 EST

  • fixe unbound onErrored handler (#104)

v1.0.1 - Thu, 30 Aug 2018 12:54:00 EST

  • Make the onChange prop not required (#102)

v1.0.0 - Fri, 17 Aug 2018 18:11:00 PST

v0.14.0 - Sun, 29 Jul 2018 19:20:03 GMT

  • 701695b [changed] dynamic url creation to allow language change

v0.13.0 - Tue, 24 Jul 2018 21:50:35 GMT

  • fde0d51 [fixed] Update async-script to get rid of Map polyfill

v0.12.0 - Mon, 11 Jun 2018 16:39:53 GMT

  • f465421 [fixed] react-async-script to dependency
  • 48a5726 [added] Parameter to use recaptchanet instead of google.com

v0.11.1 - Thu, 10 May 2018 22:28:19 GMT

  • b2ae438 [fixed] issue grecaptcha.render is not a function by adding condition

v0.11.0 - Sun, 25 Mar 2018 20:09:27 GMT

  • 4920312 Add es2015 module build (#59)

v0.10.0 - Sun, 25 Mar 2018 20:02:26 GMT

  • 8c6a8dc Remove rendered DOM on unmount (#73)

v0.9.8 - Tue, 28 Nov 2017 04:07:05 GMT

  • 7cd1254 Fix peerDependency of react

v0.9.7 - Thu, 10 Aug 2017 00:04:43 GMT

  • e5f6fd9 Add an accessor for widgetid (#53)
  • 8932900 Fixed PropTypes warning due to extra space (#51)

v0.9.6 - Tue, 20 Jun 2017 16:20:26 GMT

  • 9796adb Fix race on execute() (#49)

v0.9.5 - Fri, 02 Jun 2017 09:26:50 GMT

  • Fixed release

v0.9.4 - Fri, 02 Jun 2017 09:26:02 GMT

  • 89a1c0e Changed ref to use function instead of dep strings
  • 5c8a04d Update examples in readme (#47)
  • 349aaa8 Modified description of badge parameter (#46)

v0.9.3 - Mon, 24 Apr 2017 16:31:56 GMT

  • cb041a3 [fixed] issue with handleExpired not being bound

v0.9.2 - Thu, 20 Apr 2017 01:10:09 GMT

  • 1c8d411 [fixed] updated async-script

v0.9.1 - Tue, 18 Apr 2017 08:27:28 GMT

  • 242faf5 [fixed] space in peer dep version

v0.9.0 - Sun, 16 Apr 2017 23:36:41 GMT

  • bbf5312 [changed] updated for react 15.5

v0.8.1 - Mon, 03 Apr 2017 04:34:00 GMT

  • dbd3a47 [fixed] react-async-script dep version

v0.8.0 - Fri, 24 Mar 2017 00:54:19 GMT

  • 883210e Added support for badge attribute (#36)

v0.7.0 - Thu, 16 Mar 2017 17:11:15 GMT

  • 4eda897 Added Invisible example to README.md
  • 34d5e0c Add invisible props and execute method (#34)
  • cbfe092 [added] install of react-async-script in readme

v0.6.0 - Sun, 05 Mar 2017 02:45:00 GMT

  • 3c92d6d [changed] Updated babel, react and dropped 0.10, 0.12 (#30)
  • 7c8424c make dependencies more friendly for consuming packages (#29)

v0.5.4 - Tue, 19 Jul 2016 21:46:12 GMT

  • cb679d7 [fixed] issue with react 15.2 warnings for unknown props

v0.5.3 - Tue, 03 May 2016 01:57:36 GMT

  • 50e770f [added] temporary solution to the lang issue

v0.5.2 - Sat, 28 Nov 2015 00:47:54 GMT

v0.5.1 - Fri, 06 Nov 2015 16:17:16 GMT

  • b7cfa5c [fixed] handle widgetId equal to 0

v0.5.0 - Thu, 15 Oct 2015 21:38:43 GMT

  • d217dd1 [changed] updated all deps
  • fc3350a [added] mt-changelog and release-script
  • dfa9bf2 [added] doc for react 0.14 + old 0.13

0.4.0

  • Added Size Props |Merge #5
  • Fixed bug with refs
  • Bumps deps

0.3.2

  • Bump deps

0.3.1

  • Added babel runtime to deps
  • [#1] Removed unused use strict
  • Bump deps

0.3.0

  • Can now uses the recaptcha functions getValue reset directly from Wrapper without getComponent first.
  • bump deps

0.2.2

  • Bump react-async-script (bug 0.2.1)

0.2.1

  • Small lint fixes
  • Updated react-async-script

0.2.0

  • Now loads the script using react-async-script! Usage is now way simpler

0.1.0

  • Migrated to ES6
  • Added Travis
  • Added Babel
  • Added Karma
  • Added First test

0.0.1

  • Initial commit of component