简体   繁体   English

谷歌 reCaptcha 在 React.js 中工作?

[英]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.我非常接近......加载元素很好,因为reCaptcha脚本的工作方式使得它在创建'g-recaptcha'类的实例时不会完成渲染. SO.所以。 The reCaptcha WILL load (every time/functionally correct) if I use...如果我使用,reCaptcha 将加载(每次/功能正确)...

// 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).不仅如此......我已经在初始文档的头部放置了一个 reCaptcha 脚本(这只是一遍又一遍地重新煽动它)。 Using the libraries API and resetting the reCaptcha doesn't work (see below)...使用库 API 并重置 reCaptcha 不起作用(见下文)...

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.所以我需要以某种方式访问​​ React 中 reCaptcha div 的脚本方法。 Contact.js is an incredibly basic React component. Contact.js 是一个非常基础的 React 组件。 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.如果有人可以为我提供一些指导,那么即使只是了解在 React 组件中包含 3rd 方脚本的正确方法也会有很大帮助。

Does this seem to be on the right trail (link: https://discuss.reactjs.org/t/using-external-script-libraries/2256 )?这似乎是在正确的道路上(链接: https : //discuss.reactjs.org/t/using-external-script-libraries/2256 )?

You can use https://github.com/appleboy/react-recaptcha您可以使用https://github.com/appleboy/react-recaptcha

This library works fine.这个库工作正常。 Here's the usage:这是用法:

<Recaptcha sitekey="" 
    render="explicit" 
    hl={"ja"} 
    onloadCallback={} 
    verifyCallback={*} />

For those looking this thread and don't want to use a third party library, I came up with a auto load ReCaptcha component, no third party dependencies, hope it helps 对于那些希望使用此线程并且不想使用第三方库的人,我想出了一个自动加载ReCaptcha组件,没有第三方依赖项,希望对您有所帮助

Gist: 要旨:

https://gist.github.com/ivansnek/8b69055ddef6b6a43769ed95bd7360f8 https://gist.github.com/ivansnek/8b69055ddef6b6a43769ed95bd7360f8

CodeSandbox: CodeSandbox:

https://codesandbox.io/embed/patient-shape-k8r1l?fontsize=14 https://codesandbox.io/embed/ Patient-shape-k8r1l?fontsize = 14

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM