admin管理员组文章数量:1316844
I am working with the react-google-recaptcha to implement the invisible ReCaptcha, passing a ref to the ReCAPTCHA ponent and executing this._reCaptchaRef.current.execute()
inside the ponentDidMount. showcases a form that makes use of the reCaptcha.
the onChange callback passed to the captcha ponent will set the value of the captcha into the state. The initial render the captcha value is set into the state and everything seems to work just fine, click on the submit button and the state value is printed onto the console.
After a few seconds, if we click on submit and look at the console, the value of the captcha is null. I tried to pare the value passed to onChange and if it was null I would invoke the
this._reCaptchaRef.current.execute()
but the issue raises when the value is null and we invoke the function but the first on submit
, the value of captcha is null and every click from it will have the value in the state until it bees null.
how do I invoke the Recaptcha for the submit button and also be able to pass the value of the captcha to the callback function?
I am working with the react-google-recaptcha to implement the invisible ReCaptcha, passing a ref to the ReCAPTCHA ponent and executing this._reCaptchaRef.current.execute()
inside the ponentDidMount. https://stackblitz./edit/react-invisbile-recaptcha showcases a form that makes use of the reCaptcha.
the onChange callback passed to the captcha ponent will set the value of the captcha into the state. The initial render the captcha value is set into the state and everything seems to work just fine, click on the submit button and the state value is printed onto the console.
After a few seconds, if we click on submit and look at the console, the value of the captcha is null. I tried to pare the value passed to onChange and if it was null I would invoke the
this._reCaptchaRef.current.execute()
but the issue raises when the value is null and we invoke the function but the first on submit
, the value of captcha is null and every click from it will have the value in the state until it bees null.
how do I invoke the Recaptcha for the submit button and also be able to pass the value of the captcha to the callback function?
Share Improve this question asked Feb 6, 2020 at 9:25 visizkyvisizky 7612 gold badges11 silver badges31 bronze badges2 Answers
Reset to default 5The general idea is that the recaptcha token is valid only for a period of time. This is so that the tokens are not easily guessable by other puter systems.
Instead of doing the captcha on mount, you are supposed to execute it onSubmit
only, hence the user would have filled the form when they see the captcha if at all.
handleSubmit(event) {
event.preventDefault();
this._reCaptchaRef.current.execute()
}
This would in turn trigger the onChange
handler (or the onError
handler) and you can submit the form from there.
But if you would like to somehow, keep it in ponentDidMount
, you can choose to reset the captcha and execute it again.
redoCaptcha() {
this._reCaptchaRef.current.reset();
this._reCaptchaRef.current.execute();
}
render() {
...
<ReCAPTCHA
onExpired={this.redoCaptcha}
/>
}
import ReCAPTCHA from 'react-google-recaptcha';
const DELAY = 1500;
class Form extends React.Component {
constructor (props) {
super(props);
this.state = {
value: '',
load: false,
expired: 'false',
recaptchaLoaded: false
};
this._onSubmit = this._onSubmit.bind(this);
this._reCaptchaRef = React.createRef();
}
ponentDidMount () {
setTimeout(() => {
this.setState({load: true});
}, DELAY);
}
sendData (endpoint, formData) {
//--
//---
//---
newFormData.append('recaptcha-token', this.state.value);
//----
}
handleChange (value) {
this.setState({value});
if (this.state.value === null) this.setState({expired: 'true'});
}
asyncScriptOnLoad () {
this.setState({recaptchaLoaded: true});
}
@validateAll
async _onSubmit (valid, e) {
e.preventDefault();
await this._reCaptchaRef.current.executeAsync();
//----
//---
}
render () {
{this.state.load && googleSiteKey && (
<ReCAPTCHA
style={{display: 'inline-block'}}
theme="dark"
size="invisible"
ref={this._reCaptchaRef}
sitekey={googleSiteKey}
onChange={this.handleChange.bind(this)}
asyncScriptOnLoad={this.asyncScriptOnLoad.bind(this)}
/>
)}
<button className="cta--primary" disabled={!this.state.recaptchaLoaded}>{hasForm && (<Icon icon="rightarrow" fillColor="#fff" />)}{submit}</button>
//------
//---
}
module.exports = Form;
//-----------------------------------------------------
//for old react version remove React.createRef();
//add below line:
import ReCAPTCHA from 'react-google-recaptcha';
const DELAY = 1500;
class Form extends React.Component {
constructor (props) {
super(props);
this.state = {
value: '',
load: false,
expired: 'false',
recaptchaLoaded: false
};
this._onSubmit = this._onSubmit.bind(this);
}
ponentDidMount () {
setTimeout(() => {
this.setState({load: true});
}, DELAY);
}
sendData (endpoint, formData) {
//--
//---
//---
const newFormData = new FormData();
newFormData.append('recaptcha-token', this.state.value);
}
handleChange (value) {
this.setState({value});
if (this.state.value === null) this.setState({expired: 'true'});
}
asyncScriptOnLoad () {
this.setState({recaptchaLoaded: true});
}
@validateAll
async _onSubmit (valid, e) {
e.preventDefault();
await this.recaptchaRef.executeAsync();
//----
//---
}
render () {
{this.state.load && googleSiteKey && (
<ReCAPTCHA
style={{display: 'inline-block'}}
theme="dark"
size="invisible"
ref={(el) => { this.recaptchaRef = el; }}
sitekey={googleSiteKey}
onChange={this.handleChange.bind(this)}
asyncScriptOnLoad={this.asyncScriptOnLoad.bind(this)}
/>
)}
<button className="cta--primary" disabled={!this.state.recaptchaLoaded}>{hasForm && (<Icon icon="rightarrow" fillColor="#fff" />)}{submit}</button>
//------
//---
}
本文标签: javascriptReact Invisible reCAPTCHAStack Overflow
版权声明:本文标题:javascript - React Invisible reCAPTCHA - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742011571a2412979.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论