How to use Google ReCaptcha with React 🎉

Elina Hovakimyan
May 30, 2018 · 4 min read

When there was a need to integrate google recaptcha to our react app, we had hard times finding the best solution that will fit to all of our needs. We decided to create a new library that will give more freedom, be more flexible, and be easy to use simultaneously. We are Codeep team and this is the guide of how to use `react-recaptcha-google` in your projects.

Currently, we are using ReCaptcha V2 here. ReCaptcha V3 is still in beta version; we will update our component when they release the stable version.

P.S. ReCaptcha window will be popped up only when there are some doubts by Google; otherwise, it will automatically generate the recaptcha token.


Installation

Using npm:

npm install react-recaptcha-google --save

Using yarn:

yarn add react-recaptcha-google


How to use?

The component consists of two main parts — initializing and integrating.

This function should be imported and called in the main (parent) component of your app. We recommend calling it in componentDidMount() of App.js.

import { loadReCaptcha } from 'react-recaptcha-google'...componentDidMount() {
loadReCaptcha();
}

After initializing you can use ReCaptcha in all child components without any worries. The advantage of having this function is that you can control when you need to load ReCaptcha and when you want prevent additional calls, e.g. disable ReCaptcha locally.

react-recaptcha-google can be used both for visible and invisible recaptcha. The difference between usage is tiny. You can use the visible one in one component and the invisible one in the next easily.

Appears on the bottom right of pages using invisible ReCaptcha

For testing the invisible ReCaptcha, create a new component with the following code and give it a try! You may see nothing popping up, but it does not mean ReCaptcha is not working. Just open the console and see that it generated a new token, trusting that you are a human :))

import React, { Component } from 'react';
import { ReCaptcha } from 'react-recaptcha-google'
class ExampleComponent extends Component {
constructor(props, context) {
super(props, context);
this.onLoadRecaptcha = this.onLoadRecaptcha.bind(this);
this.verifyCallback = this.verifyCallback.bind(this);
}
componentDidMount() {
if (this.captchaDemo) {
console.log("started, just a second...")
this.captchaDemo.reset();
this.captchaDemo.execute();
}
}
onLoadRecaptcha() {
if (this.captchaDemo) {
this.captchaDemo.reset();
this.captchaDemo.execute();
}
}
verifyCallback(recaptchaToken) {
// Here you will get the final recaptchaToken!!!
console.log(recaptchaToken, "<= your recaptcha token")
}
render() {
return (
<div>
{/* You can replace captchaDemo with any ref word */}
<ReCaptcha
ref={(el) => {this.captchaDemo = el;}}
size="invisible"
render="explicit"
sitekey="your_site_key"
onloadCallback={this.onLoadRecaptcha}
verifyCallback={this.verifyCallback}
/>
<code>
1. Add <strong>your site key</strong> in the ReCaptcha component. <br/>
2. Check <strong>console</strong> to see the token.
</code>
</div>
);
};
};
export default ExampleComponent;
Source: https://developers.google.com/recaptcha/

For having a visible ReCaptcha, you should make two minor changes on the above-mentioned code.

  1. Replace the size prop value invisible (see the imported ReCaptcha component) with either normal or compact. Those will add a checkbox with 'I am not a robot' label.
  2. Remove this.[captchaRef].execute() lines from your code.

Basically, the code will look like this:

import React, { Component } from 'react';
import { ReCaptcha } from 'react-recaptcha-google'
class ExampleComponent extends Component {
constructor(props, context) {
super(props, context);
this.onLoadRecaptcha = this.onLoadRecaptcha.bind(this);
this.verifyCallback = this.verifyCallback.bind(this);
}
componentDidMount() {
if (this.captchaDemo) {
console.log("started, just a second...")
this.captchaDemo.reset();
}
}
onLoadRecaptcha() {
if (this.captchaDemo) {
this.captchaDemo.reset();
}
}
verifyCallback(recaptchaToken) {
// Here you will get the final recaptchaToken!!!
console.log(recaptchaToken, "<= your recaptcha token")
}
render() {
return (
<div>
{/* You can replace captchaDemo with any ref word */}
<ReCaptcha
ref={(el) => {this.captchaDemo = el;}}
size="normal"
data-theme="dark"
render="explicit"
sitekey="your_site_key"
onloadCallback={this.onLoadRecaptcha}
verifyCallback={this.verifyCallback}
/>
<code>
1. Add <strong>your site key</strong> in the ReCaptcha component. <br/>
2. Check <strong>console</strong> to see the token.
</code>
</div>
);
};
};
export default ExampleComponent;

Optional props

  • data-theme - you can add theme prop with a value of either "dark" or "light"(default) to control the background theme of the visible ReCaptcha (when size is normal or compact)
  • data-badge - you can send badge prop with one of the following values: bottomright (default), bottomleft, inline. This will allowyou to reposition the ReCaptcha badge.

You’re ready to go! Don’t hesitate to ask questions and contribute!

Codeep.io

Web, Mobile, Cyber Security, AI

Elina Hovakimyan

Written by

I write about latest tech news, features and useful updates. I am mainly focused on JavaScript topics, also productivity tips for everyone.

Codeep.io

Codeep.io

Web, Mobile, Cyber Security, AI

More From Medium

Also tagged JavaScript

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade