Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google reCaptcha to work in React.js?

I'm getting so close... loading the element in is fine EXCEPT for the fact that something in the way the reCaptcha script works makes it so that rendering isn't done when you create an instance of the 'g-recaptcha' class. SO. The reCaptcha WILL load (every time/functionally correct) if I use...

// the div that contains the reCaptcha is in < Contact / >
ReactDOM.render( < Contact / > , document.getElementById('non-header'));

// adding another recaptcha/api.js to the head
var imported = document.createElement('script');
imported.src = 'https://www.google.com/recaptcha/api.js';
document.head.appendChild(imported);

This is clearly a horrendous solution as every time 'Contact' is loaded another script is appended to the head. Not only that... there's already a reCaptcha script I put in the head in the initial document (this is just re-instigating it over and over). Using the libraries API and resetting the reCaptcha doesn't work (see below)...

ReactDOM.render( < Contact / > , document.getElementById('non-header'));

//var imported = document.createElement('script');
//imported.src = 'https://www.google.com/recaptcha/api.js';
//document.head.appendChild(imported);

grecaptcha.reset();


74:2  error  'grecaptcha' is not defined  no-undef !!!

So somehow I need to access the script methods for the reCaptcha div within React. Contact.js is an incredibly basic React component. It may as well just be...

import React, { Component } from 'react';
import './css/Contact.css';

class Contact extends Component {
    render() {
        return (
            <div className='g-recaptcha' id='recaptcha' data-sitekey='******************************'></div>
        );
    }
}

Maybe even just getting an idea of the proper way to include 3rd party scripts in a React Component would help a lot if anyone can provide me some guidance.

Does this seem to be on the right trail (link: https://discuss.reactjs.org/t/using-external-script-libraries/2256)?

like image 733
jscul Avatar asked Mar 01 '17 10:03

jscul


People also ask

How do I add a Google reCAPTCHA in react form?

Install react-google-recaptcha Once installed, import ReCAPTCHA from 'react-google-recaptcha' . To render the component, all you need are the following props: sitekey — API client key from Google. onChange — callback function when the user completes the captcha.

How do I enable Google reCAPTCHA?

Add Google reCAPTCHA to a formClick the pencil icon or Edit on the form or newsletter block. In the Storage tab, click Google reCAPTCHA. Switch the Use Google reCAPTCHA toggle on. Repeat these steps for all form blocks on your site where you want to add a reCAPTCHA.

Is reCAPTCHA v2 better than v3?

Is reCAPTCHA v3 better than v2? Neither of them is good at blocking bots. While reCAPTCHA v3 is less intrusive than v2 for a user, it places a significant burden on the webmaster to determine when to let users through and when to block or challenge them. There's no right answer to this.


1 Answers

You can use https://github.com/appleboy/react-recaptcha

This library works fine. Here's the usage:

<Recaptcha sitekey="" 
    render="explicit" 
    hl={"ja"} 
    onloadCallback={} 
    verifyCallback={*} />
like image 109
Vahap Gencdal Avatar answered Oct 20 '22 01:10

Vahap Gencdal