【发布时间】:2020-01-14 10:17:13
【问题描述】:
我有一个使用 create-react-app 创建的 React 应用。我将 reCaptcha 放在上面并放在下面,一切都很好。令牌回来了,我可以嵌套一个获取并继续我的生活。
componentDidMount(){
if(!this.state.isRecaptchaReady){
window.grecaptcha.ready(function(){
window.grecaptcha.execute('id', {action: 'homepage'})
.then(//...)
});
}
}
除了我不能,因为我知道必须有更好的方法。我想获取令牌并存储它,然后从中调用一个函数,而不是嵌套太深,我无法获得this 来挽救我的生命(我的意思是我可以但不想使用一堆绑定)。
requestRecaptchToken = () => {
console.log(window.grecaptcha);
//const tokenPromise = window.grecaptcha.execute('id', {action: 'homepage'});
}
handleRecaptchToken = data => {
const { modal } = this.state;
modal.recaptcha = JSON.parse(data);
this.setState({modal});
}
componentDidMount(){
if(!this.state.isRecaptchaReady){
window.grecaptcha.ready(this.handleRecaptchReady());
}
}
componentDidUpdate(){
if(this.state.isRecaptchaReady){
this.requestRecaptchToken();
}
}
这一行
const tokenPromise = window.grecaptcha.execute('id', {action: 'homepage'});
外面
window.grecaptcha.ready()
与
错误:引发了跨域错误。 React 无法访问开发中的实际错误对象。请参阅https://reactjs.org/docs/cross-origin-errors.html 了解更多信息。
问题是它没有意义,我阅读了一些 WebPack eval 的东西,但目前这似乎有点超出我的学习曲线。
【问题讨论】:
标签: javascript reactjs recaptcha